php 如何获得某个角色后的所有内容?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11405493/
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
How to get everything after a certain character?
提问by user1048676
I've got a string and I'd like to get everything after a certain value. The string always starts off with a set of numbers and then an underscore. I'd like to get the rest of the string after the underscore. So for example if I have the following strings and what I'd like returned:
我有一个字符串,我想在某个值之后获得所有内容。字符串总是以一组数字开始,然后是下划线。我想在下划线之后得到字符串的其余部分。例如,如果我有以下字符串以及我想要返回的内容:
"123_String" -> "String"
"233718_This_is_a_string" -> "This_is_a_string"
"83_Another Example" -> "Another Example"
How can I go about doing something like this?
我该如何去做这样的事情?
回答by databyss
The strpos()finds the offset of the underscore, then substr grabs everything from that index plus 1, onwards.
该strpos()发现偏移下划线,那么SUBSTR抓住一切事情,从指数加1日起实施。
$data = "123_String";
$whatIWant = substr($data, strpos($data, "_") + 1);
echo $whatIWant;
If you also want to check if the underscore character (_) exists in your string before trying to get it, you can use the following:
如果您还想_在尝试获取字符串之前检查下划线字符 ( ) 是否存在,您可以使用以下命令:
if (($pos = strpos($data, "_")) !== FALSE) {
$whatIWant = substr($data, $pos+1);
}
回答by mike rodent
strtokis an overlooked function for this sort of thing. It is meant to be quite fast.
strtok是这类事情的一个被忽视的功能。这意味着相当快。
$s = '233718_This_is_a_string';
$firstPart = strtok( $s, '_' );
$allTheRest = strtok( '' );
Empty string like this will force the rest of the string to be returned.
像这样的空字符串将强制返回字符串的其余部分。
NB if there was nothing at all after the '_' you would get a FALSEvalue for $allTheRestwhich, as stated in the documentation, must be tested with ===, to distinguish from other falsy values.
注意,如果 '_' 之后什么都没有,你会得到一个FALSE值$allTheRest,如文档中所述,必须用 === 测试该值,以区别于其他虚假值。
回答by kenorb
Here is the method by using explode:
这是使用的方法explode:
$text = explode('_', '233718_This_is_a_string', 2)[1]; // Returns This_is_a_string
or:
或者:
$text = @end((explode('_', '233718_This_is_a_string', 2)));
By specifying 2for the limitparameter in explode(), it returns array with 2 maximum elements separated by the string delimiter. Returning 2nd element ([1]), will give the rest of string.
通过指定2为limit在参数explode(),则返回用2层由字符串分隔符分隔最大的元件阵列。返回第二个元素 ( [1]),将给出字符串的其余部分。
Here is another one-liner by using strpos(as suggested by @flu):
$needle = '233718_This_is_a_string';
$text = substr($needle, (strpos($needle, '_') ?: -1) + 1); // Returns This_is_a_string
回答by Ady
if anyone needs to extract the first part of the string then can try,
如果有人需要提取字符串的第一部分,那么可以尝试,
Query:
询问:
$s = "This_is_a_string_233718";
$text = $s."_".substr($s, 0, strrpos($s, "_"));
Output:
输出:
This_is_a_string
this_is_a_string
回答by nggit
回答by Ali A. Dhillon
$string = "233718_This_is_a_string";
$withCharacter = strstr($string, '_'); // "_This_is_a_string"
echo substr($withCharacter, 1); // "This_is_a_string"
In a single statement it would be.
在一个单一的声明中。
echo substr(strstr("233718_This_is_a_string", '_'), 1); // "This_is_a_string"

