有没有办法检测通过输入字段输入的数据的语言?
10 回答
嗯,我可以提供 DimaKrasun 功能的改进版本:
functoin is_arabic($string) {
if($string === 'arabic') {
return true;
}
return false;
}
好吧,笑话够了!
Pekkas 建议使用谷歌翻译 api 是一个很好的建议!但是您依赖的外部服务总是更复杂等。
我认为Rushyos方法很好!它只是不是那么容易。我为您编写了以下功能,但未经测试,但它应该可以工作......
<?
function uniord($u) {
// i just copied this function fron the php.net comments, but it should work fine!
$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;
}
function is_arabic($str) {
if(mb_detect_encoding($str) !== 'UTF-8') {
$str = mb_convert_encoding($str,mb_detect_encoding($str),'UTF-8');
}
/*
$str = str_split($str); <- this function is not mb safe, it splits by bytes, not characters. we cannot use it
$str = preg_split('//u',$str); <- this function woulrd probably work fine but there was a bug reported in some php version so it pslits by bytes and not chars as well
*/
preg_match_all('/.|\n/u', $str, $matches);
$chars = $matches[0];
$arabic_count = 0;
$latin_count = 0;
$total_count = 0;
foreach($chars as $char) {
//$pos = ord($char); we cant use that, its not binary safe
$pos = uniord($char);
echo $char ." --> ".$pos.PHP_EOL;
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;
}
$arabic = is_arabic('عربية إخبارية تعمل على مدار اليوم. يمكنك مشاهدة بث القناة من خلال الموقع');
var_dump($arabic);
?>
最后的想法:如你所见,我添加了一个拉丁计数器,范围只是一个虚拟数字,但这样你就可以检测字符集(希伯来语、拉丁语、阿拉伯语、印地语、中文等)
您可能还想先消除一些字符......也许@、空格、换行符、斜杠等...... preg_split 函数的 PREG_SPLIT_NO_EMPTY 标志会很有用,但由于这个错误我没有在这里使用它。
您也可以为所有字符集设置一个计数器,然后查看哪个字符集最多...
最后,您应该考虑在 200 个字符或其他内容后将字符串切掉。这应该足以说明使用了什么字符集。
你必须做一些错误处理!比如除以零,空字符串等!请不要忘记...有任何问题吗?评论!
如果要检测字符串的 LANGUAGE,则应拆分为单词并检查某些预定义表中的单词。您不需要完整的字典,只需要最常用的单词就可以了。标记化/规范化也是必须的!无论如何都有图书馆,这不是你要求的:)只是想提一下
这将检查字符串是阿拉伯语还是有阿拉伯语文本
文本必须是 UNICODE,例如 UTF-8
$str = "بسم الله";
if (preg_match('/[اأإء-ي]/ui', $str)) {
echo "A match was found.";
} else {
echo "A match was not found.";
}
使用正则表达式来获得更简短的答案
$is_arabic = preg_match('/\p{Arabic}/u', $text);
这将返回真 (1) 用于阿拉伯字符串和 0 用于非阿拉伯字符串
我假设在 99% 的情况下,检查字符串是否包含阿拉伯字母并且不包含所有字母就足够了。
我的核心假设是,如果它包含至少两个或三个阿拉伯字母,读者应该知道如何阅读它。
您可以使用一个简单的功能:
<?php
/**
* Return`s true if string contains only arabic letters.
*
* @param string $string
* @return bool
*/
function contains_arabic($string)
{
return (preg_match("/^\p{Arabic}/i", $string) > 0);
}
或者,如果正则表达式类不起作用:
function contains_arabic($subject)
{
return (preg_match("/^[\x0600-\x06FF]/i", $subject) > 0);
}
我会使用正则表达式来获取阿拉伯字符的数量并将其与字符串的总长度进行比较。例如,如果文本至少有 60% 的阿拉伯语字符,我会认为它主要是阿拉伯语并应用 RTL 格式。
/**
* Is the given text mainly Arabic language?
*
* @param string $text string to be tested if it is arabic. :-)
* @return bool
*/
function ct_is_arabic_text($text) {
$text = preg_replace('/[ 0-9\(\)\.\,\-\:\n\r_]/', '', $text); // Remove spaces, numbers, punctuation.
$total_count = mb_strlen($text); // Length of text
if ($total_count==0)
return false;
$arabic_count = preg_match_all("/[اأإء-ي]/ui", $text, $matches); // Number of Arabic characters
if(($arabic_count/$total_count) > 0.6) { // >60% Arabic chars, its probably Arabic languages
return true;
}
return false;
}
对于内联 RTL 格式,请使用 CSS。示例类:
.embed-rtl {
direction: rtl;
unicode-bidi: normal;
text-align: right;
}
我假设您指的是 Unicode 字符串……在这种情况下,只需查找字符串中是否存在代码在 U+0600–U+06FF (1536–1791) 之间的任何字符。
public static function isArabic($string){
if(preg_match('/\p{Arabic}/u', $string))
return true;
return false;
}
PHP Text_LanguageDetect 库能够检测 52 种语言。它是通过 composer 和 PEAR 进行单元测试和安装的。
此功能检查输入的行/句子是否为阿拉伯语。我先修剪它,然后逐字检查计算两者的总数。
function isArabic($string){
// Initializing count variables with zero
$arabicCount = 0;
$englishCount = 0;
// Getting the cleanest String without any number or Brackets or Hyphen
$noNumbers = preg_replace('/[0-9]+/', '', $string);
$noBracketsHyphen = array('(', ')', '-');
$clean = trim(str_replace($noBracketsHyphen , '', $noNumbers));
// After Getting the clean string, splitting it by space to get the total entered words
$array = explode(" ", $clean); // $array contain the words that was entered by the user
for ($i=0; $i <= count($array) ; $i++) {
// Checking either word is Arabic or not
$checkLang = preg_match('/\p{Arabic}/u', $array[$i]);
if($checkLang == 1){
++$arabicCount;
} else{
++$englishCount;
}
}
if($arabicCount >= $englishCount){
// Return 1 means TRUE i-e Arabic
return 1;
} else{
// Return 0 means FALSE i-e English
return 0;
}
}