I'm trying to grab a specific part of a string which has a start charecter and an end charecter.
For example;
$str = "Lorem ipsum |example xyz|dolor sit amet, consectetur adipiscing
elit |example|. Vivamus at posuere magna. Suspendisse rutrum rhoncus leo
vitae vehicula. Nunc nec dapibus nisi. Donec facilisis mauris sapien, eget
blandit enim |example xyz| dignissim auctor.|example| Nullam a porta orci.
Donec pharetra urna odio, ut pellentesque est eleifend vel. Nulla tincidunt
tellus sed dui fermentum viverra. Vivamus sit amet semper mi."
I want grab parts all |example xyz| .. blabla.. |example|
.
How can I do this ? Thank you..
function stringcutout($string,$start,$stop)
{
$ret='';
if (strstr($string,$start))
{
$p=strpos($string,$start);
$b=substr($string,$p+strlen($start));
if (strstr($b,$stop))
{
$p=strpos($b,$stop);
$b=substr($b,0,$p);
$ret=$b;
}
}
return $ret;
}
I'm using this. I know it's not pretty, but working ...
Use regex for that \|([\w\s]+)\|
to get any words between |
UPDATE
To include |example xyz|
and |example|
use this regex:
(\|example xyz\|[\w\s]\|example\|)
If you have a fixed string that you're looking to find you could use preg_match.
preg_match_all('/\|example xyz\|[\w\s\,\.]+\|example\|/', $str, $matches);
if (count($matches) > 0)
{
foreach ($matches[0] as $match)
{
echo $match;
}
}
Ok, so you need a different function ... You can get multiple contents including anything starts with |example with this function:
$return=array();
$ex=explode('|example',$str);
if (sizeof($ex)>0) {unset($ex[0]); foreach ($ex as $s) {$x=strpos($s,'|'); $return[]=substr($s,$x+1);}}
if you don't want the extra last result (which is not "terminated" by |example), you can unset it ...