note 105004 deleted from function.str-word-count by danbrown

From: Date: Mon, 25 Jul 2011 12:32:27 +0000
Subject: note 105004 deleted from function.str-word-count by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-181845@lists.php.net to get a copy of this message
Note Submitter: Stano110 at azet dot sk ---- 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); ?>

« previous php.notes (#181845) next »