note 105004 added to function.str-word-count
| From: | Stano110 at azet dot sk | Date: | Thu, 21 Jul 2011 18:59:29 +0000 |
| Subject: | note 105004 added to function.str-word-count | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-181765@lists.php.net to get a copy of this message | ||
this is another version of algorithm for counting words, that uses all special chars from the first
half of ascii table as delimiters. works quite good with utf-8:
<?php
function count_words($string)
{
//$string = htmlspecialchars_decode(strip_tags($string)); //optional
if (strlen($string)==0)
return 0;
for ($i=0; $i<256; $i++)
$d[$i] = (($i>0 && $i<=47) || ($i>=58 && $i<=64) || ($i>=91
&& $i<=96) || ($i>=123 && $i<=127))? true:false; // delimiters from ascii
table are set true
$count= (!$d[ord($string[0])] || ord($string[0])>255) ? 1:0;
for ($i=1; $i<strlen($string); $i++)
{
$cp= ord($string[$i-1]);
$c = ord($string[$i]);
$cp= ($cp>255 || $d[$cp]==false)? false:true;
$c = ($c>255 || $d[$c]==false)? false:true;
if ($cp && !$c) // if new word starts
$count++;
}
return $count;
}
$str='skúška správnosti algoritmu';
echo count_words($str);
?>
----
Server IP: 69.147.83.197
Probable Submitter: 80.87.209.10
----
Manual Page -- http://www.php.net/manual/en/function.str-word-count.php
Edit -- https://master.php.net/note/edit/105004
Del: integrated -- https://master.php.net/note/delete/105004/integrated
Del: useless -- https://master.php.net/note/delete/105004/useless
Del: bad code -- https://master.php.net/note/delete/105004/bad+code
Del: spam -- https://master.php.net/note/delete/105004/spam
Del: non-english -- https://master.php.net/note/delete/105004/non-english
Del: in docs -- https://master.php.net/note/delete/105004/in+docs
Del: other reasons-- https://master.php.net/note/delete/105004
Reject -- https://master.php.net/note/reject/105004
Search -- https://master.php.net/manage/user-notes.php