PHP mb_strpos无法识别版权符号


PHP mb_strpos not recognizing copyright symbol

我试图使用mb_strpos PHP函数来搜索某些网页上的©符号。

       $pagecontent = file_get_contents($website_url);
        if (mb_strpos($pagecontent, $string_to_find) === false) {
                // String / Content NOT found on page (FAIL)
                return false;
        } else {
                // String / Content FOUND on page (SUCCESS)
                return true;
        }

我会传递$website_url$string_to_find "Copyright©"变量到函数,但是它返回false,即使我知道©存在于网页中。如果我从字符串中删除©字符,那么它返回true..所以我猜有一个问题,PHP试图找到©符号?

谁能给我指个正确的方向?

既然您正在使用mb_strpos,我假设您设置了编码与mb_internal_encoding ?否则你可以直接用strpos

那么,网站的编码是什么呢?你的"内部编码"是什么?我打赌它们不匹配。

。如果网站使用UTF-8编码,则可以使用

mb_strpos($pagecontent, $string_to_find, 0, "utf-8")

此外,布尔值也是值,因此您可以将代码简化为

$pagecontent = file_get_contents($website_url);
return (mb_strpos($pagecontent, $string_to_find) !== false);

完整的解决方案是:

$pagecontent = file_get_contents($website_url);
return (mb_strpos($pagecontent, $string_to_find, 0, "utf-8") !== false);

假设网站使用UTF-8。另外,您必须确保$string_to_find具有相同的编码。如果将Copyright符号作为字符串文字放入代码中(如"©"),则源文件也应该采用UTF-8编码。在PHP中,字符串在内部只是字节流。


其他可能性(我刚刚读了上面的评论):网站包含一个© HTML实体。在这种情况下,您必须搜索©