PHP 子字符串提取。获取第一个 '/' 之前的字符串或整个字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1935918/
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
PHP substring extraction. Get the string before the first '/' or the whole string
提问by gnud
I am trying to extract a substring. I need some help with doing it in PHP.
我正在尝试提取一个子字符串。我需要一些帮助来用 PHP 做这件事。
Here are some sample strings I am working with and the results I need:
以下是我正在使用的一些示例字符串以及我需要的结果:
home/cat1/subcat2 => home
test/cat2 => test
startpage => startpage
I want to get the string till the first /, but if no /is present, get the whole string.
我想获取字符串直到第一个/,但如果不/存在,则获取整个字符串。
I tried,
我试过,
substr($mystring, 0, strpos($mystring, '/'))
I think it says - get the position of /and then get the substring from position 0 to thatposition.
我认为它说 - 获取位置,/然后从位置 0 到该位置获取子字符串。
I don't know how to handle the case where there is no /, without making the statement too big.
我不知道如何处理没有的情况/,而不会使声明太大。
Is there a way to handle that case also without making the PHP statement too complex?
有没有办法在不使 PHP 语句过于复杂的情况下处理这种情况?
回答by jmarceli
回答by gnud
回答by AndyBrandy
$first = explode("/", $string)[0];
回答by IRCF
What about this :
那这个呢 :
substr($mystring.'/', 0, strpos($mystring, '/'))
Simply add a '/' to the end of mystring so you can be sure there is at least one ;)
只需在 mystring 的末尾添加一个“/”,这样您就可以确定至少有一个 ;)
回答by glyph
One-line version of the accepted answer:
已接受答案的单行版本:
$out=explode("/", $mystring, 2)[0];
Should work in php 5.4+
应该在 php 5.4+ 中工作
回答by pavlovich
This is probably the shortest example that came to my mind:
这可能是我想到的最短的例子:
list($first) = explode("/", $mystring);
1) list()will automatically assign string until "/"if delimiter is found
2) if delimiter "/"is not found then the whole string will be assigned
1)list()将自动分配字符串,直到"/"找到分隔符
2) 如果"/"未找到分隔符,则将分配整个字符串
...and if you get really obsessed with performance, you may add extra parameter to explode explode("/", $mystring, 2)which limits maximum of the returned elements.
...如果你真的对性能着迷,你可以添加额外的参数来explode("/", $mystring, 2)限制返回元素的最大值。
回答by aimme
Late is better than never. php has a predefined function for that. here is that good way.
迟到总比不到好。php 有一个预定义的函数。这就是好方法。
strstr
if you want to get the part before match just set before_needle(3rd parameter) to truehttp://php.net/manual/en/function.strstr.php
如果您想在匹配前获取零件,只需将before_needle(第三个参数)设置为truehttp://php.net/manual/en/function.strstr.php
function not_strtok($string, $delimiter)
{
$buffer = strstr($string, $delimiter, true);
if (false === $buffer) {
return $string;
}
return $buffer;
}
var_dump(
not_strtok('st/art/page', '/')
);
回答by Ronald
The function strstr() in PHP 5.3 should do this job.. The third parameter however should be set to true..
PHP 5.3 中的 strstr() 函数应该可以完成这项工作.. 然而,第三个参数应该设置为 true..
But if you're not using 5.3, then the function below should work accurately:
但是如果你没有使用 5.3,那么下面的函数应该可以正常工作:
function strbstr( $str, $char, $start=0 ){
if ( isset($str[ $start ]) && $str[$start]!=$char ){
return $str[$start].strbstr( $str, $char, $start+1 );
}
}
I haven't tested it though, but this should work just fine.. And it's pretty fast as well
我还没有测试过,但这应该可以正常工作......而且它也非常快
回答by t00ny
You can try using a regex like this:
您可以尝试使用这样的正则表达式:
$s = preg_replace('|/.*$|', '', $s);
sometimes, regex are slower though, so if performance is an issue, make sure to benchmark this properly and use an other alternative with substrings if it's more suitable for you.
有时,正则表达式虽然较慢,但如果性能是一个问题,请确保对其进行适当的基准测试,并使用其他更适合您的子字符串替代方案。
回答by t00ny
You could create a helper function to take care of that:
您可以创建一个辅助函数来解决这个问题:
/**
* Return string before needle if it exists.
*
* @param string $str
* @param mixed $needle
* @return string
*/
function str_before($str, $needle)
{
$pos = strpos($str, $needle);
return ($pos !== false) ? substr($str, 0, $pos) : $str;
}
Here's a use case:
这是一个用例:
$sing = 'My name is Luka. I live on the second floor.';
echo str_before($sing, '.'); // My name is Luka

