PHP substr 在某个字符之后,一个 substr + strpos 优雅的解决方案?

发布于 2024-09-29 04:58:05 字数 605 浏览 1 评论 0原文

假设我想返回来自

$source_str = "Tuex helo babe" 的某个针字符 'x' 之后的所有字符。

通常我会这样做:

if( ($x_pos = strpos($source_str, 'x')) !== FALSE )
   $source_str = substr($source_str, $x_pos + 1);

你知道更好/更智能(更优雅的方式)来做到这一点吗?

如果不使用正则表达式,那不会使它更优雅,而且可能还会更慢。

不幸的是我们不能这样做:

$source_str = substr(source_str, strpos(source_str, 'x') + 1);

因为当找不到 'x' 时,strpos 返回 FALSE (而不是 -1就像 JS 中一样)。 FALSE 将计算为零,并且第一个字符将始终被切断。

谢谢,

let's say I want to return all chars after some needle char 'x' from:

$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.

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦在深巷 2024-10-06 04:58:06

$item 末尾附加一个 '-',这样它总是在 '-' 之前返回字符串,即使 $item 不包含 '-',因为 strpos 默认返回'-' 第一次出现的位置

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

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 occurrence of '-'.

substr($item,0,strpos($item.'-','-'))
对你再特殊 2024-10-06 04:58:05

您的第一种方法很好:检查 x 是否包含在 strpos 中,如果包含,则使用 substr 在其后面获取任何内容。

但您也可以使用 strstr

strstr($str, 'x')

但因为这会返回以 开头的子字符串 < code>x,使用substr来获取x之后的部分:

if (($tmp = strstr($str, 'x')) !== false) {
    $str = substr($tmp, 1);
}

但这要复杂得多。因此,请改用 strpos 方法。

Your first approach is fine: Check whether x is contained with strpos and if so get anything after it with substr.

But you could also use strstr:

strstr($str, 'x')

But as this returns the substring beginning with x, use substr to get the part after x:

if (($tmp = strstr($str, 'x')) !== false) {
    $str = substr($tmp, 1);
}

But this is far more complicated. So use your strpos approach instead.

权谋诡计 2024-10-06 04:58:05

正则表达式会让它变得更加优雅:

// helo babe
echo preg_replace('~.*?x~', '', $str);

// Tuex helo babe
echo preg_replace('~.*?y~', '', $str);

但你总是可以尝试这个:

// 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);

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);
烦人精 2024-10-06 04:58:05
if(strpos($source_str, 'x') !== FALSE )
   $source_str = strstr($source_str, 'x');

不太优雅,但开头没有 x

if(strpos($source_str, 'x') !== FALSE )
   $source_str = substr(strstr($source_str, 'x'),1);
if(strpos($source_str, 'x') !== FALSE )
   $source_str = strstr($source_str, 'x');

Less elegant, but without x in the beginning:

if(strpos($source_str, 'x') !== FALSE )
   $source_str = substr(strstr($source_str, 'x'),1);
浮生面具三千个 2024-10-06 04:58:05

我只需要这个,并且为了好玩而努力将其保留在一行上:

ltrim(strstr($source_str, $needle = "x") ?: $source_str, $needle);

三元运算符在 5.3 中进行了修改,以使其能够工作。

从 PHP 5.3 开始,可以省略三元运算符的中间部分。如果 expr1 计算结果为 TRUE,则表达式 expr1 ?: expr3 返回 expr1,否则返回 expr3。

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

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);

The ternary operator was adapted in 5.3 to allow this to work.

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.

NB. ltrim will trim multiple matching characters at the start of the string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文