Creating Blog Summary text on blogList.php

3 posts by 2 authors in: Forums > CMS Builder
Last Post: September 7, 2011   (RSS)

Re: [gversion] Creating Blog Summary text on blogList.php

By Jason - September 7, 2011

Hi Greg,

You can use a function that has been developed called maxWords. You give this function your content and the maximum number of words you want displayed, and it returns the shortened text.

Here is the function:

<?php

function maxWords($textOrHtml, $maxWords) {

$text = strip_tags($textOrHtml, "<b></b><i></i>");
$words = preg_split("/\s+/", $text, $maxWords+1);

if (count($words) > $maxWords) { unset($words[$maxWords]); }
$output = join(' ', $words);

return $output;
}

?>


You can put this somewhere on your blogList.php page. To keep your code neat, it's a good idea to put functions down at the bottom of the page.

You can then call the function like this:

Example:
<?php echo maxWords($record['content'], 20); ?>

This code will print the first 20 words of the content field to the screen.

Hope this helps.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] Creating Blog Summary text on blogList.php

By gversion - September 7, 2011

Hi Jason,

That is absolutely superb! Thank you so much for the quick and well explained response.

Regards,
Greg