note 72710 added to function.str-word-count

From: Date: Tue, 30 Jan 2007 16:15:01 +0000
Subject: note 72710 added to function.str-word-count
Groups: php.notes 
Request: Send a blank email to php-notes+get-122368@lists.php.net to get a copy of this message
Here is a php work counting function together with a javascript version which will print the same result. <?php //Php word counting function function word_count($theString) { $char_count = strlen($theString); $fullStr = $theString." "; $initial_whitespace_rExp = "^[[:alnum:]]$"; $left_trimmedStr = ereg_replace($initial_whitespace_rExp,"",$fullStr); $non_alphanumerics_rExp = "^[[:alnum:]]$"; $cleanedStr = ereg_replace($non_alphanumerics_rExp," ",$left_trimmedStr); $splitString = explode(" ",$cleanedStr); $word_count = count($splitString)-1; if(strlen($fullStr)<2) { $word_count=0; } return $word_count; } ?> <?php //Function to count words in a phrase function wordCount(theString) { var char_count = theString.length; var fullStr = theString + " "; var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi; var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, ""); var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9]+/gi; var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " "); var splitString = cleanedStr.split(" "); var word_count = splitString.length -1; if (fullStr.length <2) { word_count = 0; } return word_count; } ?> ---- Server IP: 64.71.164.2 Probable Submitter: 82.71.67.46 ---- Manual Page -- http://www.php.net/manual/en/function.str-word-count.php Edit -- https://master.php.net/note/edit/72710 Del: integrated -- https://master.php.net/note/delete/72710/integrated Del: useless -- https://master.php.net/note/delete/72710/useless Del: bad code -- https://master.php.net/note/delete/72710/bad+code Del: spam -- https://master.php.net/note/delete/72710/spam Del: non-english -- https://master.php.net/note/delete/72710/non-english Del: in docs -- https://master.php.net/note/delete/72710/in+docs Del: other reasons-- https://master.php.net/note/delete/72710 Reject -- https://master.php.net/note/reject/72710 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#122368) next »