php 从字符串中获取字符串后面的字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4221117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get the string after a string from a string
提问by Alex
what's the fastest way to get only the important_stuffpart from a string like this:
从这样的字符串中只获取important_stuff部分的最快方法是什么:
bla-bla_delimiter_important_stuff
_delimiter_
is always there, but the rest of the string can change.
_delimiter_
始终存在,但字符串的其余部分可以更改。
回答by jon_darkstar
here:
这里:
$arr = explode('delimeter', $initialString);
$important = $arr[1];
回答by zemagel
$result = end(explode('_delimiter_', 'bla-bla_delimiter_important_stuff'));
回答by Yair Budic
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')
1) 对于 substr 字符串从 '0' 开始,而对于 strpos & strlen 取字符串的大小(从 '1' 开始)
2) using 1 character delimiter maybe a good idea
2) 使用 1 个字符分隔符可能是个好主意
回答by Hamish
$importantStuff = array_pop(explode('_delimiter_', $string));
回答by Byron Whitlock
$string = "bla-bla_delimiter_important_stuff";
list($junk,$important_stufF) = explode("_delimiter_",$string);
echo $important_stuff;
> important_stuff