limiting text output
| From: | chris schneck | Date: | Thu, 19 Jul 2001 14:34:22 +0000 |
| Subject: | limiting text output | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-58697@lists.php.net to get a copy of this message | ||
To anyone else who had the same problem, I thought i'd share the solution
that was passed on to me. It was suggested that counting the # of characters
in a string would work, and it does, but a better way to do it as not to cut
yourself off in mid sentence is to count the # of actual words.
-- snipity snip --
function make_newsbite($body, $len){ // $body is
article $len is maxwords
$words = Array();
preg_match_all('/([^ ]*) */', $body, $words); // separate
string into words array
for($n = 0; $n <= $len; $n++){ // for count words to len
$newsbite .= $words[0][$n] . ' '; // append words
to $newsbite
}
return $newsbite;
}
-- snipity snip --
use it like
$lilnews = make_newsbite($bignews, 80);
Hope this solution helps others.
A big thanks to Don Read for this code.
if ( strlen($foo) > 100) {
echo substr($foo,0,100), ' <A HREF="/showmore.php">More
...</A>';
} else {
echo $foo;
}