0

我有一个来自 PHP.net 的示例来检测阿拉伯语。为什么它不起作用?

$text = 'بيتر هو صبي.'; // arabic
//$text = 'פיטר הוא ילד.'; // hebrew

mb_regex_encoding('UTF-8');

if(mb_ereg('[\x{0600}-\x{06FF}]', $text)) // arabic range
//if(mb_ereg('[\x{0590}-\x{05FF}]', $text)) // hebrew range
{
    echo "Text has some arabic/hebrew characters.";
}
else
{
    echo "Text doesnt have arabic/hebrew characters.";
}
4

1 回答 1

1

您不应该依赖字符范围来检测阿拉伯字符。您可以直接\p{Arabic}用于检测阿拉伯字母:

以下工作正常:

$text = 'بيتر هو صبي.'; // arabic
//$text = 'פיטר הוא ילד.'; // hebrew
if (preg_match('~\p{Arabic}~u', $text))
    echo "Text has some arabic/hebrew characters.";
else
    echo "Text doesnt have arabic/hebrew characters.";

输出:

Text has some arabic/hebrew characters.
于 2013-10-02T15:53:43.857 回答