How to display first X characters?

4 posts by 3 authors in: Forums > CMS Builder
Last Post: August 26, 2008   (RSS)

By samsam92 - August 23, 2008

Hi there, I was wondering how I could display on the listing page the first X characters of a field such as "content" ?

For example: <?php echo $record['content'] ?> I want to show the first 100 characters.

Let me know please

Thanks a lot

Re: [samsam92] How to display first X characters?

By samsam92 - August 23, 2008

Found it!

Just add at the top of the page:

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 where you want to display your content:

<?php echo textLimit($record['content'], 20) ?>

Re: [samsam92] How to display first X characters?

By zip222 - August 26, 2008

Awesome! That is super useful.