str_word_count和阿拉伯文字

时间:2014-09-02 20:16:18

标签: php count

我使用函数str_word_count来计算文本中有多少个ARABIC单词,但它返回零:

$sentence = 'بِسْمِ اللَّهِ الرَّحْمَٰنِ الرَّحِيمِ';
$countSentence = str_word_count($sentence);
echo 'Total words '.$countSentence.'<br />';

提前致谢

1 个答案:

答案 0 :(得分:0)

尝试使用此功能

if (!function_exists('utf8_str_word_count')){
     function utf8_str_word_count($string, $format = 0, $charlist = null) {
            if ($charlist === null) {
                $regex = '/\\pL[\\pL\\p{Mn}\'-]*/u';
            }
            else {
                $split = array_map('preg_quote',
                preg_split('//u',$charlist,-1,PREG_SPLIT_NO_EMPTY));
                $regex = sprintf('/(\\pL|%1$s)([\\pL\\p{Mn}\'-]|%1$s)*/u',
                implode('|', $split));
            }
            switch ($format) {
                default:
                case 0:
                    // For PHP >= 5.4.0 this is fine:
                    return preg_match_all($regex, $string);
        
                    // For PHP < 5.4 it's necessary to do this:
                    // $results = null;
                    // return preg_match_all($regex, $string, $results);
                case 1:
                    $results = null;
                    preg_match_all($regex, $string, $results);
                    return $results[0];
                case 2:
                    $results = null;
                    preg_match_all($regex, $string, $results, PREG_OFFSET_CAPTURE);
                    return empty($results[0])
                            ? array()
                            : array_combine(
                                array_map('end', $results[0]),
                                array_map('reset', $results[0]));
            }
         }
       }

示例

$sentence = 'بِسْمِ اللَّهِ الرَّحْمَٰنِ الرَّحِيمِ';
$countSentence = utf8_str_word_count($sentence);
echo 'Total words '.$countSentence.'<br />';
相关问题