Alex
Alex

Reputation: 68406

Get the string after a string from a string

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

Answers (6)

Adrian Goia
Adrian Goia

Reputation: 171

Me, being a REGEX enthusiast:

if (preg_match('/.*_delimiter_(.*)/', 'bla-bla_delimiter_important_stuff', $matches)) echo $matches[1];

Upvotes: 1

Yair Budic
Yair Budic

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

zemagel
zemagel

Reputation: 379

$result = end(explode('_delimiter_', 'bla-bla_delimiter_important_stuff'));

Upvotes: 37

jon_darkstar
jon_darkstar

Reputation: 16768

here:

$arr = explode('delimeter', $initialString);
$important = $arr[1];

Upvotes: 78

Hamish
Hamish

Reputation: 23316

$importantStuff = array_pop(explode('_delimiter_', $string));

Upvotes: 6

Byron Whitlock
Byron Whitlock

Reputation: 53851

$string = "bla-bla_delimiter_important_stuff";
list($junk,$important_stufF) = explode("_delimiter_",$string);

echo $important_stuff;
> important_stuff

Upvotes: 6

Related Questions