note 79699 added to function.str-word-count

From: Date: Sun, 09 Dec 2007 04:01:08 +0000
Subject: note 79699 added to function.str-word-count
Groups: php.notes 
Request: Send a blank email to php-notes+get-132964@lists.php.net to get a copy of this message
<?php /** * Returns the number of words in a string. * As far as I have tested, it is very accurate. * The string can have HTML in it, * but you should do something like this first: * * $search = array( * '@<script[^>]*?>.*?</script>@si', * '@<style[^>]*?>.*?</style>@siU', * '@<![\s\S]*?--[ \t\n\r]*>@' * ); * $html = preg_replace($search, '', $html); * */ function word_count($html) { # strip all html tags $wc = strip_tags($html); # remove 'words' that don't consist of alphanumerical characters or punctuation $pattern = "#[^(\w|\d|\'|\"|\.|\!|\?|;|,|\\|\/|\-|:|\&|@)]+#"; $wc = trim(preg_replace($pattern, " ", $wc)); # remove one-letter 'words' that consist only of punctuation $wc = trim(preg_replace("#\s*[(\'|\"|\.|\!|\?|;|,|\\|\/|\-|:|\&|@)]\s*#", " ", $wc)); # remove superfluous whitespace $wc = preg_replace("/\s\s+/", " ", $wc); # split string into an array of words $wc = explode(" ", $wc); # remove empty elements $wc = array_filter($wc); # return the number of words return count($wc); } ?> ---- Server IP: 208.69.120.35 Probable Submitter: 66.16.195.194 ---- Manual Page -- http://www.php.net/manual/en/function.str-word-count.php Edit -- https://master.php.net/note/edit/79699 Del: integrated -- https://master.php.net/note/delete/79699/integrated Del: useless -- https://master.php.net/note/delete/79699/useless Del: bad code -- https://master.php.net/note/delete/79699/bad+code Del: spam -- https://master.php.net/note/delete/79699/spam Del: non-english -- https://master.php.net/note/delete/79699/non-english Del: in docs -- https://master.php.net/note/delete/79699/in+docs Del: other reasons-- https://master.php.net/note/delete/79699 Reject -- https://master.php.net/note/reject/79699 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#132964) next »