DISPLAYING THE FIRST X CHARACTERS ON A LISTING PAGE - Feb 27th, 2011


SAMSAM92 asked: If, for example I have a field <?PHP echo $record['your_field'] ?>, how would I show only the first 100
characters?

And the answer found was:

Just add this function at the top of the page:



<?PHP function textLimit($string, $length, $replacer = '...')
{
if(
strlen($string) > $length)
return (
preg_match('/^(.*)\W.*$/', substr($string, 0, $length+1), $matches) ? $matches[1] : substr($string, 0, $length))
.
$replacer;

return
$string;
}

?>



and then inside the foreach loop where you want to display your content:



<?PHP echo textLimit($record['your_field'], 100) ?>



You can change the 100 to any character count that you want.

Note:

If you're using this for a URL or anything with either a "." (period) or a "/" (forward slash) it won't work.

As soon as there's a fix, I'll post it here



The materials on this web site have been created for use with CMS Builder content management software. CMS Builder software is published and licensed for use by InteractiveTools.com. Please contact Interactive Tools for information on the downloading of the software or the purchasing of licenses.


Terms of Service