note 109722 modified in function.str-word-count by aharvey

From: Date: Wed, 15 Aug 2012 01:51:17 +0000
Subject: note 109722 modified in function.str-word-count by aharvey
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-190073@lists.php.net to get a copy of this message
Here is a count words function which supports UTF-8 and Hebrew. I tried other functions but they don&#39;t work. Notice that in Hebrew, &#39;&#34;&#39; and &#39;\\&#39;&#39; can be used in words, so they are not separators. This function is not perfect, I would prefer a function we are using in JavaScript which considers all characters except [a-zA-Zא-ת0-9_\\&#39;\\&#34;] as separators, but I don&#39;t know how to do it in PHP.&#13;&#10;&#13;&#10;I removed some of the separators which don&#39;t work well with Hebrew (&#34;\\x20&#34;, &#34;\\xA0&#34;, &#34;\\x0A&#34;, &#34;\\x0D&#34;, &#34;\\x09&#34;, &#34;\\x0B&#34;, &#34;\\x2E&#34;). I also removed the underline.&#13;&#10;&#13;&#10;&#60;?php&#13;&#10;&#13;&#10;function count_words($string) {&#13;&#10;&#9;// Return the number of words in a string.&#13;&#10;&#9;$string= str_replace(&#34;&#39;&#34;, &#34;&#39;&#34;, $string);&#13;&#10; $t= array(&#39; &#39;, &#34;\\t&#34;, &#39;=&#39;, &#39;+&#39;, &#39;-&#39;, &#39;*&#39;, &#39;/&#39;, &#39;\\\\&#39;, &#39;,&#39;, &#39;.&#39;, &#39;;&#39;, &#39;:&#39;, &#39;[&#39;, &#39;]&#39;, &#39;{&#39;, &#39;}&#39;, &#39;(&#39;, &#39;)&#39;, &#39;&#60;&#39;, &#39;&#62;&#39;, &#39;&#38;&#39;, &#39;%&#39;, &#39;$&#39;, &#39;@&#39;, &#39;#&#39;, &#39;^&#39;, &#39;!&#39;, &#39;?&#39;, &#39;~&#39;); // separators&#13;&#10;&#9;$string= str_replace($t, &#34; &#34;, $string);&#13;&#10;&#9;$string= trim(preg_replace(&#34;/\\s+/&#34;, &#34; &#34;, $string));&#13;&#10;&#9;$word_array= explode(&#34; &#34;, $string);&#13;&#10;&#9;$num= count($word_array);&#13;&#10;&#9;return $num;&#13;&#10;}&#13;&#10;&#13;&#10;?&#62; --was-- Here is a count words function which supports UTF-8 and Hebrew. I tried other functions but they don't work. Notice that in Hebrew, '"' and '\'' can be used in words, so they are not separators. This function is not perfect, I would prefer a function we are using in JavaScript which considers all characters except [a-zA-Zא-ת0-9_\'\"] as separators, but I don't know how to do it in PHP. I removed some of the separators which don't work well with Hebrew ("\x20", "\xA0", "\x0A", "\x0D", "\x09", "\x0B", "\x2E"). I also removed the underline. <?php function count_words($string) { // Return the number of words in a string. $string= str_replace("&#039;", "'", $string); $t= array(' ', "\t", '=', '+', '-', '*', '/', '\\', ',', '.', ';', ':', '[', ']', '{', '}', '(', ')', '<', '>', '&', '%', '$', '@', '#', '^', '!', '?'); // separators $string= str_replace($t, " ", $string); $string= trim(preg_replace("/\s+/", " ", $string)); $word_array= explode(" ", $string); $num= count($word_array); return $num; } ?> http://php.net/manual/en/function.str-word-count.php

« previous php.notes (#190073) next »