note 57902 deleted from function.count by levim

From: Date: Thu, 10 Jan 2013 14:23:36 +0000
Subject: note 57902 deleted from function.count by levim
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-192880@lists.php.net to get a copy of this message
Note Submitter: Fred D ---- 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 <?php 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; } ?>

« previous php.notes (#192880) next »