/**
* 是否包含阿拉伯语
* @param $str
* @return bool
*/
function is_include_arabic($str)
{
if (preg_match('/[أ-ي]/ui', $str)) {
return TRUE;
}
return FALSE;
}
/**
* 是否为阿拉伯语
* @param $str
* @return bool
*/
public function is_arabic($str)
{
if (mb_detect_encoding($str) !== 'UTF-8') {
$str = mb_convert_encoding($str, mb_detect_encoding($str), 'UTF-8');
}
preg_match_all('/.|\n/u', $str, $matches);
$chars = $matches[0];
$arabic_count = 0;
$latin_count = 0;
$total_count = 0;
foreach ($chars as $char) {
$pos = uniord($char);
if ($pos >= 1536 && $pos <= 1791) {
$arabic_count++;
} else if ($pos > 123 && $pos < 123) {
$latin_count++;
}
$total_count++;
}
if (($arabic_count / $total_count) > 0.6) {
// 60% arabic chars, its probably arabic
return true;
}
return false;
}
/**
* @param $u
* @return float|int
*/
function uniord($u)
{
$k = mb_convert_encoding($u, 'UCS-2LE', 'UTF-8');
$k1 = ord(substr($k, 0, 1));
$k2 = ord(substr($k, 1, 1));
return $k2 * 256 + $k1;
}