Supporting special characters with str_word_count()

前端 未结 4 817
醉梦人生
醉梦人生 2021-02-20 06:36

The str_word_count() function returns an array that holds all words in a string. It works great, except when using special characters. In this case, the php script receives the

4条回答
  •  说谎
    说谎 (楼主)
    2021-02-20 06:57

    Not sure if that third parameter is sufficient to make str_word_count work for non-ASCII symbols. It probably only works with Latin-1 if anything.

    As alternative you could count the words with a regex however:

    $count = preg_match_all('/\pL+/u', $_GET['q'], $matches);
    

    This works for UTF-8 at least. To fully replicate str_word_count you might need [\pL']+ eventually.

提交回复
热议问题