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

时间:2021-03-06 23:28:17

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.

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

5 个解决方案

#1


54  

here:

在这里:

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

#2


31  

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

#3


6  

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

#4


5  

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

echo $important_stuff;
> important_stuff

#5


5  

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


54  

here:

在这里:

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

#2


31  

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

#3


6  

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

#4


5  

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

echo $important_stuff;
> important_stuff

#5


5  

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)使用一个字符分隔符可能是个好主意。