关于 substr() 的帮助

发布于 2024-11-05 13:09:14 字数 442 浏览 3 评论 0原文

我想删除该字符串中 #blitz 之前的所有内容:

$twit = rt @danisan01: #blitz ipva em frente ao barra sul, no recreio.

这是我正在尝试的内容,但在输出中没有得到任何结果:

$array_bols = array("#bols", "#blitz", "#blitz ipva", "#ipva", "#detran", "#blitz de ipva", "#detran ipva", "#blitz d ipva");

foreach($array_bols as $blitz)
{
$twit = substr(strstr($twit, $blitz), strlen($blitz), (-1) * strlen($twit));
}

help

I want to remove everything before #blitz in this string:

$twit = rt @danisan01: #blitz ipva em frente ao barra sul, no recreio.

here's what I'm trying but I get no results on the output:

$array_bols = array("#bols", "#blitz", "#blitz ipva", "#ipva", "#detran", "#blitz de ipva", "#detran ipva", "#blitz d ipva");

foreach($array_bols as $blitz)
{
$twit = substr(strstr($twit, $blitz), strlen($blitz), (-1) * strlen($twit));
}

help

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

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

发布评论

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

评论(1

权谋诡计 2024-11-12 13:09:14

您不会得到任何输出,因为您迭代了搜索词列表。并且您的 $twit 变量将在某个时候被清空,因为如果 strstr 找不到搜索的主题,则不会返回任何内容。

您想要做的如下:

$array_bols = array("#bols", "#blitz", "#blitz ipva", "#ipva", "#detran", "#blitz de ipva", "#detran ipva", "#blitz d ipva");

foreach($array_bols as $blitz)
{
    if ($tmp = strstr($twit, $blitz)) {
        $twit = substr($tmp, strlen($blitz));
    }
}

内部 substr 还删除了 #blitz 因为我认为这就是您的代码的用途。请注意在这种情况下如何省略 substr() 长度参数。

You get no output, because you iterate over a list of search words. And your $twit variable will be emptied at some point, because strstr returns nothing if it can't find the searched subject.

What you wanted to do is following:

$array_bols = array("#bols", "#blitz", "#blitz ipva", "#ipva", "#detran", "#blitz de ipva", "#detran ipva", "#blitz d ipva");

foreach($array_bols as $blitz)
{
    if ($tmp = strstr($twit, $blitz)) {
        $twit = substr($tmp, strlen($blitz));
    }
}

The inner substr also removes the #blitz as I assume that's what your code was for. Note how you can leave out the substr() length parameter in such cases.

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