Get the string after a string from a string

PhpString

Php Problem Overview


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.

Php Solutions


Solution 1 - Php

here:

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

Solution 2 - Php

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

Solution 3 - Php

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

Solution 4 - Php

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

Solution 5 - Php

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

echo $important_stuff;
> important_stuff

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlexView Question on Stackoverflow
Solution 1 - Phpjon_darkstarView Answer on Stackoverflow
Solution 2 - PhpzemagelView Answer on Stackoverflow
Solution 3 - PhpYair BudicView Answer on Stackoverflow
Solution 4 - PhpHamishView Answer on Stackoverflow
Solution 5 - PhpByron WhitlockView Answer on Stackoverflow