Reputation: 68406
what's the fastest way to get only the important_stuff part from a string like this:
bla-bla_delimiter_important_stuff
_delimiter_
is always there, but the rest of the string can change.
Upvotes: 52
Views: 84458
Reputation: 171
Me, being a REGEX enthusiast:
if (preg_match('/.*_delimiter_(.*)/', 'bla-bla_delimiter_important_stuff', $matches)) echo $matches[1];
Upvotes: 1
Reputation: 121
I like this method:
$str="bla-bla_delimiter_important_stuff";
$del="_delimiter_";
$pos=strpos($str, $del);
cutting from end of the delimiter to end of string
$important=substr($str, $pos+strlen($del)-1, strlen($str)-1);
note:
1) for substr the string start at '0' whereas for strpos & strlen takes the size of the string (starts at '1')
2) using 1 character delimiter maybe a good idea
Upvotes: 8
Reputation: 379
$result = end(explode('_delimiter_', 'bla-bla_delimiter_important_stuff'));
Upvotes: 37
Reputation: 16768
here:
$arr = explode('delimeter', $initialString);
$important = $arr[1];
Upvotes: 78
Reputation: 53851
$string = "bla-bla_delimiter_important_stuff";
list($junk,$important_stufF) = explode("_delimiter_",$string);
echo $important_stuff;
> important_stuff
Upvotes: 6