Limit amount of text shown
23 posts by 6 authors in: Forums > CMS Builder
Last Post: November 17, 2010 (RSS)
By grauchut - October 29, 2008
<?php echo substr($record['content'], 0, 150);?>
Re: [grauchut] Limit amount of text shown
By Dave - October 30, 2008
Often if you look in the comments on php.net for a function people will post other solutions for things like this.
Check out http://www.php.net/substr (just replace substr to look up any other function)
They have this one:
<?php
function cutText($string, $setlength) {
$length = $setlength;
if($length<strlen($string)){
while (($string{$length} != " ") AND ($length > 0)) {
$length--;
}
if ($length == 0) return substr($string, 0, $setlength);
else return substr($string, 0, $length);
}else return $string;
}
?>
Which you could use like this:
<?php echo cutText($record['content'], 0, 150); ?>
Hope that helps!
interactivetools.com
Re: [Dave] Limit amount of text shown
By grauchut - October 30, 2008
<?php
function cutText($string, $setlength) {
$length = $setlength;
if($length<strlen($string)){
while (($string{$length} != " ") AND ($length > 0)) {
$length--;
}
if ($length == 0) return substr($string, 0, $setlength);
else return substr($string, 0, $length);
}else return $string;
}
?>
Re: [grauchut] Limit amount of text shown
By Dave - October 30, 2008
interactivetools.com
Re: [Dave] Limit amount of text shown
By grauchut - October 30, 2008
Re: [grauchut] Limit amount of text shown
By Dave - October 31, 2008
<?php echo cutText($record['content'], 150); ?>
interactivetools.com
Re: [Dave] Limit amount of text shown
By grauchut - November 1, 2008
Re: [grauchut] Limit amount of text shown
By Dave - November 3, 2008
Also, here's a function I wrote for Donna that limits by words:
// Usage: < ?php print maxWords($content, 25); ? >
function maxWords($textOrHtml, $maxWords) {
$text = strip_tags($textOrHtml);
$words = preg_split("/\s+/", $text, $maxWords+1);
if (count($words) > $maxWords) { unset($words[$maxWords]); }
$output = join(' ', $words);
return $output;
}
Maybe that one will work better?
interactivetools.com
Re: [Dave] Limit amount of text shown
By grauchut - November 3, 2008
Re: [grauchut] Limit amount of text shown
By Dave - November 3, 2008
interactivetools.com