note 57902 added to function.count

From: Date: Tue, 18 Oct 2005 16:16:21 +0000
Subject: note 57902 added to function.count
Groups: php.notes 
Request: Send a blank email to php-notes+get-97176@lists.php.net to get a copy of this message
The trim_text function was helpful, but it did not take account of the possibility of having nothing to trim which can sometimes happen if you are using this function in a loop through data. I've added a count function to deal with that possibility ------------------------------ function trim_text_elipse($text, $count){ //Create variable $trimmed=""; //Remove double white space $text = str_replace(" ", " ", $text); //Turn the text into an array $string = explode(" ", $text); //Check to see how many words there are $wordTotal = count($string); //Check to see if there are more words than the $count variable if($wordTotal > $count){ //Loop through adding words until the $count variable is reached for ( $wordCounter = 0; $wordCounter <= $count; $wordCounter++ ){ $trimmed .= $string[$wordCounter]; //Check to and add space or finish with elipse if ( $wordCounter < $count ){ $trimmed .= " "; } else { $trimmed .= " &#8230;"; } } }else{ //Set value returned to the existing value $trimmed =$text; } //Trim off any white space $trimmed = trim($trimmed); return $trimmed; } ------------------------------- ---- Manual Page -- http://www.php.net/manual/en/function.count.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+57902 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+57902&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+57902&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+57902&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+57902&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+57902&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+57902&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+57902&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+57902&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#97176) next »