PHP substr 在某个字符之后,一个 substr + strpos 优雅的解决方案?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4036036/
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 substr after a certain char, a substr + strpos elegant solution?
提问by Marco Demaio
let's say I want to return all chars after some needle char 'x'
from:
假设我想从以下位置返回一些针字符后的所有字符'x'
:
$source_str = "Tuex helo babe"
.
$source_str = "Tuex helo babe"
.
Normally I would do this:
通常我会这样做:
if( ($x_pos = strpos($source_str, 'x')) !== FALSE )
$source_str = substr($source_str, $x_pos + 1);
Do you know a better/smarter (more elegant way) to do this?
你知道一个更好/更聪明(更优雅的方式)来做到这一点吗?
Without using regexp that would not make it more elegant and probably also slower.
不使用正则表达式不会让它更优雅,也可能更慢。
Unfortunately we can not do:
不幸的是,我们不能这样做:
$source_str = substr(source_str, strpos(source_str, 'x') + 1);
Because when 'x'
is not found strpos
returns FALSE
(and not -1
like in JS).
FALSE
would evaluate to zero, and 1st char would be always cut off.
因为 when 'x'
is not foundstrpos
返回FALSE
(而-1
不像在 JS 中)。
FALSE
将评估为零,并且第一个字符将始终被切断。
Thanks,
谢谢,
采纳答案by Gumbo
Your first approach is fine: Check whether x
is contained with strpos
and if so get anything after it with substr
.
您的第一种方法很好:检查是否x
包含 with strpos
,如果包含,则使用substr
.
But you could also use strstr
:
但你也可以使用strstr
:
strstr($str, 'x')
But as this returns the substring beginning withx
, use substr
to get the part after x
:
但随着这将返回子开始用x
,用substr
后得到的部分x
:
if (($tmp = strstr($str, 'x')) !== false) {
$str = substr($tmp, 1);
}
But this is far more complicated. So use your strpos
approach instead.
但这要复杂得多。因此,请改用您的strpos
方法。
回答by Alix Axel
Regexes would make it a lot more elegant:
正则表达式会让它更加优雅:
// helo babe
echo preg_replace('~.*?x~', '', $str);
// Tuex helo babe
echo preg_replace('~.*?y~', '', $str);
But you can always try this:
但是你总是可以试试这个:
// helo babe
echo str_replace(substr($str, 0, strpos($str, 'x')) . 'x', '', $str);
// Tuex helo babe
echo str_replace(substr($str, 0, strpos($str, 'y')) . 'y', '', $str);
回答by dev-null-dweller
if(strpos($source_str, 'x') !== FALSE )
$source_str = strstr($source_str, 'x');
Less elegant, but without x
in the beginning:
不太优雅,但x
一开始就没有:
if(strpos($source_str, 'x') !== FALSE )
$source_str = substr(strstr($source_str, 'x'),1);
回答by William George
I needed just this, and striving to keep it on one line for fun came up with this:
我只需要这个,并且为了好玩而努力将它保持在一行上想出了这个:
ltrim(strstr($source_str, $needle = "x") ?: $source_str, $needle);
ltrim(strstr($source_str, $needle = "x") ?: $source_str, $needle);
The ternary operator
was adapted in 5.3 to allow this to work.
将ternary operator
被改编5.3为使这一工作。
Since PHP 5.3, it is possible to leave out the middle part of the ternary operator. Expression expr1 ?: expr3 returns expr1 if expr1 evaluates to TRUE, and expr3 otherwise.
自 PHP 5.3 起,可以省略三元运算符的中间部分。表达式 expr1 ?: 如果 expr1 的计算结果为 TRUE,则 expr3 返回 expr1,否则返回 expr3。
NB. ltrim
will trim multiple matching characters at the start of the string.
注意。ltrim
将修剪字符串开头的多个匹配字符。
回答by user2673374
Append a '-' at the end of $item
so it always returns string before '-' even $item
doesn't contain '-', because strpos by default returns the position of first occurrenceof '-'.
在末尾附加一个“-”,$item
所以它总是在“-”之前返回字符串,甚至$item
不包含“-”,因为默认情况下 strpos 返回“-”第一次出现的位置。
substr($item,0,strpos($item.'-','-'))