note 57902 modified in function.count by danbrown
| From: | danbrown@php.net | Date: | Fri, 09 Jan 2009 23:00:53 +0000 |
| Subject: | note 57902 modified in function.count by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-149013@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
<?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 .= " …"; }
}
}else{
//Set value returned to the existing value
$trimmed =$text;
}
//Trim off any white space
$trimmed = trim($trimmed);
return $trimmed;
}
?>
--was--
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 .= " …"; }
}
}else{
//Set value returned to the existing value
$trimmed =$text;
}
//Trim off any white space
$trimmed = trim($trimmed);
return $trimmed;
}
-------------------------------
http://php.net/manual/en/function.count.php