2

So say I would write something like <break> and it would show everything before <break> out of

$text = "I like apple pies<break>Do you like Apple pies?";

So it should only output

I like apple pies
codaddict
  • 445,704
  • 82
  • 492
  • 529
Keverw
  • 3,736
  • 7
  • 31
  • 54

5 Answers5

5
$text = "I like apple pies<break>Do you like Apple pies?";

list($result) = explode('<break>',$text,2);
codaddict
  • 445,704
  • 82
  • 492
  • 529
  • <3, big duh on my part. Especially like the elegance of using `list()` – Kevin Peno Mar 18 '11 at 15:28
  • 3
    `list($result) = explode('',$text,2);` is more efficient, especially if the string is long after the `` mark (php doesn't have to search the whole string). – Czechnology Mar 18 '11 at 15:43
3

As a function:

function BeforeBreak($input)
{
    return stristr($input, '<break>', true);
}

Note this functionality requires PHP 5.3.0.

stristr documentation

JYelton
  • 35,664
  • 27
  • 132
  • 191
1

Try this:

$text = "I like apple pies<break>Do you like Apple pies?";
$texplode = explode('<break>',$text);
echo $texplode[0];

read up on php explode()

Naftali
  • 144,921
  • 39
  • 244
  • 303
1
$good = substr( $bad, 0, strpos( $bad, "<break>" ) );
Kevin Peno
  • 9,107
  • 1
  • 33
  • 56
1

this will give you what you want:

<?
   $output = strstr($text, '<break>', true);
?>

if you have multiple occurrences it's a good idea to use

<?
   $text = 'a<break>b<break>c<break>d';
   $output = explode('<break>', $text);

   // the output will be
   // array('a', 'b', 'c', 'd');
?>
Teneff
  • 30,564
  • 13
  • 72
  • 103
  • Please note that you will need to have PHP 5.3.0 installed to be able to use the `before_needle` parameter. Nice answer though, +1. – Michiel Pater Mar 18 '11 at 15:44