从这样的字符串中只获取important_stuff部分的最快方法是什么:
bla-bla_delimiter_important_stuff
Run Code Online (Sandbox Code Playgroud)
_delimiter_
总是存在,但字符串的其余部分可以改变.
jon*_*tar 63
这里:
$arr = explode('delimeter', $initialString);
$important = $arr[1];
Run Code Online (Sandbox Code Playgroud)
小智 32
$result = end(explode('_delimiter_', 'bla-bla_delimiter_important_stuff'));
Run Code Online (Sandbox Code Playgroud)
$importantStuff = array_pop(explode('_delimiter_', $string));
$string = "bla-bla_delimiter_important_stuff";
list($junk,$important_stufF) = explode("_delimiter_",$string);
echo $important_stuff;
> important_stuff
Run Code Online (Sandbox Code Playgroud)
小智 5
我喜欢这种方法:
$str="bla-bla_delimiter_important_stuff";
$del="_delimiter_";
$pos=strpos($str, $del);
Run Code Online (Sandbox Code Playgroud)
从分隔符的末尾切割到字符串的末尾
$important=substr($str, $pos+strlen($del)-1, strlen($str)-1);
Run Code Online (Sandbox Code Playgroud)
注意:
1)对于substr,字符串从'0'开始,而strpos&strlen取字符串的大小(从'1'开始)
2)使用1个字符分隔符可能是一个好主意