str_word_count和阿拉伯文字 [英] str_word_count and Arabic text

查看:56
本文介绍了str_word_count和阿拉伯文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I used the function str_word_count to count how many ARABIC words are in a text, but it returns zero:

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

预先感谢

推荐答案

尝试使用此功能

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 />';

这篇关于str_word_count和阿拉伯文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆