1

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

Thanks in advance

Abu Rayane
  • 241
  • 1
  • 14
  • 2
    See this Q&A http://stackoverflow.com/q/13884178/ and http://stackoverflow.com/q/8109997/ and http://stackoverflow.com/q/23015600/ – Funk Forty Niner Sep 02 '14 at 20:18
  • 2
    This works `echo 'Number of words: '.count(preg_split('/\s+/', $sentence));` borrowed from http://stackoverflow.com/a/23015645/ - `Output: Number of words: 4` – Funk Forty Niner Sep 02 '14 at 20:24
  • so str_word_count does NOT work with special characters, we should create a special function to count words – Abu Rayane Sep 02 '14 at 20:25
  • I would imagine, yes, far as I know. The answer that worked counts spaces `\s` actually between the words. – Funk Forty Niner Sep 02 '14 at 20:25
  • Thanks Fred -ii-, I already checked the above links, but I will use the above answer using preg_split, that will be better – Abu Rayane Sep 02 '14 at 20:31

1 Answers1

0

Try to use this function

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]));
            }
         }
       }

Example

$sentence = 'بِسْمِ اللَّهِ الرَّحْمَٰنِ الرَّحِيمِ';
$countSentence = utf8_str_word_count($sentence);
echo 'Total words '.$countSentence.'<br />';
Mo'men Mohamed
  • 1,851
  • 20
  • 24