note 57166 added to function.str-word-count

From: Date: Sun, 25 Sep 2005 23:58:57 +0000
Subject: note 57166 added to function.str-word-count
Groups: php.notes 
Request: Send a blank email to php-notes+get-95776@lists.php.net to get a copy of this message
Trying to make an effiecient word splitter, and "paragraph limiter", eg, limit item text to 100, or 200 words and so-forth. I don't know how well this compares, but it works nicely. function trim_text($string, $word_count=100) { $trimmed = ""; $string = preg_replace("/\040+/"," ", trim($string)); $stringc = explode(" ",$string); echo sizeof($stringc); if($word_count >= sizeof($stringc)) { // nothing to do, our string is smaller than the limit. return $string; } elseif($word_count < sizeof($stringc)) { // trim the string to the word count for($i=0;$i<$word_count;$i++) { $trimmed .= $stringc[$i]." "; } if(substr($trimmed, strlen(trim($trimmed))-1, 1) == '.') return trim($trimmed).'..'; else return trim($trimmed).'...'; } } $text = "some test text goes in here, I'm not sure, but ok."; echo trim_text($text,5); ---- Manual Page -- http://www.php.net/manual/en/function.str-word-count.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+57166 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+57166&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+57166&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+57166&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+57166&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+57166&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+57166&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+57166&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+57166&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#95776) next »