note 104839 deleted from function.str-word-count by sobak

From: Date: Sat, 10 Dec 2016 11:14:15 +0000
Subject: note 104839 deleted from function.str-word-count by sobak
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-208425@lists.php.net to get a copy of this message
Note Submitter: Stano110 at azet dot sk ---- 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; } ?>

« previous php.notes (#208425) next »