note 104839 added to function.str-word-count

From: Date: Sun, 10 Jul 2011 13:36:47 +0000
Subject: note 104839 added to function.str-word-count
Groups: php.notes 
Request: Send a blank email to php-notes+get-181404@lists.php.net to get a copy of this message
This function count words, is quick and works well with utf-8: (this is corrected version from my previous post) <?php function count_words($string) { $string = htmlspecialchars_decode(strip_tags($string)); if (strlen($string)==0) return 0; $t = array(' '=>1, '_'=>1, "\x20"=>1, "\xA0"=>1, "\x0A"=>1, "\x0D"=>1, "\x09"=>1, "\x0B"=>1, "\x2E"=>1, "\t"=>1, '='=>1, '+'=>1, '-'=>1, '*'=>1, '/'=>1, '\\'=>1, ','=>1, '.'=>1, ';'=>1, ':'=>1, '"'=>1, '\''=>1, '['=>1, ']'=>1, '{'=>1, '}'=>1, '('=>1, ')'=>1, '<'=>1, '>'=>1, '&'=>1, '%'=>1, '$'=>1, '@'=>1, '#'=>1, '^'=>1, '!'=>1, '?'=>1); // separators $count= isset($t[$string[0]])? 0:1; if (strlen($string)==1) return $count; for ($i=1;$i<strlen($string);$i++) if (isset($t[$string[$i-1]]) && !isset($t[$string[$i]])) // if new word starts $count++; return $count; } ?> ---- 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/104839 Del: integrated -- https://master.php.net/note/delete/104839/integrated Del: useless -- https://master.php.net/note/delete/104839/useless Del: bad code -- https://master.php.net/note/delete/104839/bad+code Del: spam -- https://master.php.net/note/delete/104839/spam Del: non-english -- https://master.php.net/note/delete/104839/non-english Del: in docs -- https://master.php.net/note/delete/104839/in+docs Del: other reasons-- https://master.php.net/note/delete/104839 Reject -- https://master.php.net/note/reject/104839 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#181404) next »