如何在字符串php中检测除英语以外的其他语言?

时间:2016-12-21 14:04:37

标签: php

如何在字符串php中检测除英语以外的其他语言?

我使用此代码检测英语旁边的其他语言。

我想知道如何简化代码并覆盖所有语言。

<?PHP
$utf8_str = "xxxxxxxxxxxxxxx";
$Common = preg_match("/\p{Common}+/u", $utf8_str);
$Arabic = preg_match("/\p{Arabic}+/u", $utf8_str);
$Armenian = preg_match("/\p{Armenian}+/u", $utf8_str);
$Bengali = preg_match("/\p{Bengali}+/u", $utf8_str);
$Bopomofo = preg_match("/\p{Bopomofo}+/u", $utf8_str);
$Braille = preg_match("/\p{Braille}+/u", $utf8_str);
$Buhid = preg_match("/\p{Buhid}+/u", $utf8_str);
$Canadian_Aboriginal = preg_match("/\p{Canadian_Aboriginal}+/u", $utf8_str);
$Cherokee = preg_match("/\p{Cherokee}+/u", $utf8_str);
$Cyrillic = preg_match("/\p{Cyrillic}+/u", $utf8_str);
$Devanagari = preg_match("/\p{Devanagari}+/u", $utf8_str);
$Ethiopic = preg_match("/\p{Ethiopic}+/u", $utf8_str);
$Georgian = preg_match("/\p{Georgian}+/u", $utf8_str);
$Greek = preg_match("/\p{Greek}+/u", $utf8_str);
$Gujarati = preg_match("/\p{Gujarati}+/u", $utf8_str);


if(($Common != '0') OR ($Arabic != '0') OR ($Armenian != '0') OR ($Bengali != '0') OR ($Bopomofo != '0') OR ($Braille != '0') OR ($Buhid != '0') OR ($Canadian_Aboriginal!= '0') OR ($Cherokee != '0') OR ($Cyrillic != '0') OR ($Devanagari != '0') OR ($Ethiopic != '0') OR ($Georgian != '0') OR ($Greek != '0') OR ($Gujarati != '0'))
{
    echo "have other language";
}
else
{
    echo "Only english";
}
?>

1 个答案:

答案 0 :(得分:0)

国际化需要比简单的正则表达式花费更多时间。看看这个,https://lingohub.com/blog/2013/07/internationalization-how-to-5-most-popular-php-frameworks/