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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:47:37  来源:igfitidea点击:

PHP substr after a certain char, a substr + strpos elegant solution?

phpsubstrstrpos

提问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 strposreturns FALSE(and not -1like in JS). FALSEwould 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 xis contained with strposand 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 substrto 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 strposapproach 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 xin 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 operatorwas 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. ltrimwill trim multiple matching characters at the start of the string.

注意。ltrim将修剪字符串开头的多个匹配字符。

回答by user2673374

Append a '-' at the end of $itemso it always returns string before '-' even $itemdoesn't contain '-', because strpos by default returns the position of first occurrenceof '-'.

在末尾附加一个“-”,$item所以它总是在“-”之前返回字符串,甚至$item不包含“-”,因为默认情况下 strpos 返回“-”第一次出现位置

substr($item,0,strpos($item.'-','-'))