php-从字符串中获取字符串后的字符串

从这样的字符串中仅获取Important_stuff部分的最快方法是什么:

bla-bla_delimiter_important_stuff

_delimiter_始终存在,但是字符串的其余部分可以更改。

Alex asked 2020-08-01T12:56:19Z
5个解决方案
62 votes

这里:

$arr = explode('delimeter', $initialString);
$important = $arr[1];
jon_darkstar answered 2020-08-01T12:56:33Z
32 votes
$result = end(explode('_delimiter_', 'bla-bla_delimiter_important_stuff'));
zemagel answered 2020-08-01T12:56:48Z
6 votes
$importantStuff = array_pop(explode('_delimiter_', $string));
Hamish answered 2020-08-01T12:57:04Z
5 votes
$string = "bla-bla_delimiter_important_stuff";
list($junk,$important_stufF) = explode("_delimiter_",$string);

echo $important_stuff;
> important_stuff
Byron Whitlock answered 2020-08-01T12:57:19Z
5 votes

我喜欢这种方法:

$str="bla-bla_delimiter_important_stuff";
$del="_delimiter_";
$pos=strpos($str, $del);

从定界符的末端到字符串的结尾

$important=substr($str, $pos+strlen($del)-1, strlen($str)-1);

注意:

1)对于substr,字符串从“ 0”开始,而对于strpos和strlen,使用字符串的大小(从“ 1”开始)

2)使用1个字符分隔符可能是个好主意

Yair Budic answered 2020-08-01T12:57:57Z
translate from https://stackoverflow.com:/questions/4221117/get-the-string-after-a-string-from-a-string