March 14, 2012

How to count # words in a textarea

Question by Yuliang Han

I am wondering whether there is any javascript and PHP code to count number of words which are encoded in unicode.

Answer by Starx

Picked out from the php.net’s manual

function str_word_count_utf8($str) {
  return count(preg_split('~[^p{L}p{N}']+~u',$str));
}

As it is quoted

If the pattern contains utf-8, utf8_encode() the pattern,
as it is expected to be valid utf-8 (using the u modifier).

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!