Php 获取字符串中的url

Php 获取字符串中的url,php,spam-prevention,Php,Spam Prevention,大家好,我正在尝试一个快速而肮脏的解决方案来阻止我网站上的一个评论垃圾邮件发送者,尝试在一堆文本中拾取此url“”,如下所示: if (strpos($_POST['text'], "https://www.change.org/en-GB/petitions/rockstar-games-release-games-for-linux") == true) { header("Location: /home/banned");

大家好,我正在尝试一个快速而肮脏的解决方案来阻止我网站上的一个评论垃圾邮件发送者,尝试在一堆文本中拾取此url“”,如下所示:

        if (strpos($_POST['text'], "https://www.change.org/en-GB/petitions/rockstar-games-release-games-for-linux") == true)
        {
            header("Location: /home/banned");
        }
遗憾的是,这似乎不起作用,有没有一个好的方法可以做到这一点?

来自php

strpos()

Returns the position of where the needle exists relative to the beginning of the haystack string (independent of offset). Also note that string positions start at 0, and not 1.

Returns FALSE if the needle was not found.
它不会返回
TRUE
,所以请像
那样执行它==FALSE

返回“返回指针所在位置”或“如果未找到指针,则返回FALSE”。因此,您应该测试它是否返回false:

if (strpos(...) === false) {
    // needle was not found
}
在您的情况下,您需要检查否定(即发现针):


请注意两个运算符(
=
!=
)中的提取等号,这两个运算符都可以使用。如果您不使用类型严格比较,并且在位置
0
处发现指针,则会得到假阴性结果(
0
被解释为等于
false
)。

您应该使用!==false而不是==true

   if (strpos($_POST['text'], "https://www.change.org/en-GB/petitions/rockstar-games-release-games-for-linux") !== false)
    {
        header("Location: /home/banned");
        exit;
    }

那不应该是
!==false
这仍然使我的脚本继续运行,就好像它没有被找到一样,当它清楚地出现在我要检查的文本中时。什么是$_POST['text']内容?该url只是一个测试。不可能,当我为$_POST['text']分配了与您提供的url相同的字符串时,我被重定向到禁止的url
   if (strpos($_POST['text'], "https://www.change.org/en-GB/petitions/rockstar-games-release-games-for-linux") !== false)
    {
        header("Location: /home/banned");
        exit;
    }