我在尝试比较 PHP 脚本中的两个阿拉伯语字符串以查看它们是否匹配时遇到问题。 我尝试过使用 8 mb_internal_encoding将内部编码设置为 UTF-8,我尝试了一个简单的 if ($x == $y) 表达式,我尝试了 strcmp()...没有骰子。 知道我做错了什么吗? PHP 在与非英语文本进行字符串比较时是否有问题?
谢谢!
以下是代码摘录:
// Chop up HTML content into bits
$threadPieces = explode('</div>', $innerHTML);
// Chop up the HTML bits into data entries
$strippedThreadPieces = strip_tags($threadPieces[1]);
$threadInfo = explode('-', $strippedThreadPieces);
$threadTitleExists = trim($threadTitleExists, 'thread_title_');
$postername = "مراسل";
if (($threadTitleExists > 100000) && ($threadInfo[0] === $postername))
{
echo 'Thread title:';
echo strip_tags($threadPieces[0]);
echo '<p>';
}
else
{
}
我认为问题是您的 php 文件的字符集,请尝试使用 utf-8 字符集保存它。
我这样做的方法是使用函数mb_ereg,它是具有多字节支持的正则表达式匹配。
这是我正在使用的一个
//from http://www.phperz.com/article/14/1029/31806.html
function mb_split_str($str) {
preg_match_all("/./u", $str, $arr);
return $arr[0];
}
//based on http://www.phperz.com/article/14/1029/31806.html, added percent
function mb_similar_text($str1, $str2, &$percent) {
$arr_1 = array_unique(mb_split_str($str1));
$arr_2 = array_unique(mb_split_str($str2));
$similarity = count($arr_2) - count(array_diff($arr_2, $arr_1));
$percent = ($similarity * 200) / (strlen($str1) + strlen($str2) );
return $percent;
}
所以
$var = mb_similar_text('عمار', 'ياسر', $per);
output: $var = 2, $per = 25