代码之家  ›  专栏  ›  技术社区  ›  HyderA

php:如何检测输入字符串是否为阿拉伯语

  •  16
  • HyderA  · 技术社区  · 16 年前

    是否有方法检测通过输入字段输入的数据的语言?

    9 回复  |  直到 7 年前
        1
  •  29
  •   Josh Crozier HBP    10 年前

    嗯,我可以提供Dimakrasun功能的改进版本:

    functoin is_arabic($string) {
        if($string === 'arabic') {
             return true;
        }
        return false;
    }
    

    好吧,开玩笑吧!

    佩卡斯建议使用谷歌翻译API是一个不错的一个!但你所依赖的外部服务总是比较复杂的。

    我认为拉什约斯批准是好的!只是不是那么容易。 我为您编写了以下函数,但它没有经过测试,但应该可以工作…

        <?
    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);
    ?>
    

    最后的想法: 如您所见,我添加了一个拉丁计数器,范围只是一个虚拟数字b,这样您就可以检测字符集(希伯来语、拉丁语、阿拉伯语、印地语、中文等)。

    你也可以先去掉一些字符…可能是@、空格、换行符、斜线等… preg_split_no_empty标志对于preg_split函数是有用的,但是由于这个bug,我没有在这里使用它。

    您还可以为所有字符集设置一个计数器,并查看哪个字符集是最…

    最后,你应该考虑在200个字符或其他时间后剪断你的绳子。这应该足以说明使用了什么字符集。

    你必须做一些错误处理!像被零除,空字符串等!别忘了,拜托…有什么问题吗?评论!

    如果要检测字符串的语言,应将其拆分为单词,并检查某些预定义表中的单词。你不需要一本完整的字典,只需要最常用的单词,它就可以很好地工作。标记化技术/规范化也是必须的!不管怎么说,这里有图书馆,这不是你要的:()只是想提一下。

        2
  •  8
  •   Mohammed Ahmed    7 年前

    这将检查字符串是阿拉伯语还是阿拉伯语文本

    文本必须为Unicode,例如UTF-8

    $str = "بسم الله";
    if (preg_match('/[اأإء-ي]/ui', $str)) {
        echo "A match was found.";
    } else {
        echo "A match was not found.";
    }
    
        3
  •  3
  •   Community Mohan Dere    9 年前

    您可以使用我为您编写的函数:

    <?php
    /**
     * Return`s true if string contains only arabic letters.
     *
     * @param string $string
     * @return bool
     */
    function is_arabic($string)
    {
        return (preg_match("/^\p{Arabic}/i", $string) > 0);
    }
    

    但请在使用前检查一下。

    [编辑1 ]

    您的问题:“如何检测输入字符串是否为阿拉伯语?”我已经回答过了,怎么了?

    [编辑2 ]

    阅读此 Detect language from string in PHP

    [编辑3 ]

    对不起,我把函数重写为这个,试试看:

    function is_arabic($subject)
    {
        return (preg_match("/^[\x0600-\x06FF]/i", $subject) > 0);
    }
    
        4
  •  2
  •   Mohammad Anini saravanajd    12 年前
    public static function isArabic($string){
        if(preg_match('/\p{Arabic}/u', $string))
            return true;
        return false;
    }
    
        5
  •  1
  •   Pekka    16 年前

    我不知道这方面的PHP解决方案。

    这个 Google Translate Ajax APIs 不过,可能是为了你。

    从api文档中查看此javascript片段: Example: Language Detection

        6
  •  1
  •   Rushyo    16 年前

    我想你指的是Unicode字符串…在这种情况下,只需查找字符串中代码在U+0600U+06FF(15361791)之间的任何字符。

        7
  •  1
  •   cweiske agentofuser    9 年前

    PHP Text_LanguageDetect library 能够检测52种语言。它的单元测试和安装通过作曲家和梨。

        8
  •  1
  •   Affan    8 年前

    使用正则表达式来缩短和简化答案

     $is_arabic = preg_match('/\p{Arabic}/u', $text);
    

    对于阿拉伯字符串,返回true(1),对于非阿拉伯字符串,返回0。

        9
  •  0
  •   Yasir Tahir    10 年前

    此函数检查输入的行/句是否为阿拉伯语。我先修剪它,然后逐字核对,计算两者的总数。

    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;
            }
        }
    
    推荐文章