Hiding "...read more" when using maxWords

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

Re: [gkornbluth] Hiding "...read more" when using maxWords

By Jason - September 2, 2011

Hi Jerry,

To do this type of comparison you need to be able to get the actual number of words from your original string. Here is a function that goes through the same process as maxWords, but just returns the count:

<?PHP
function wordCount($textOrHtml) {
$text = strip_tags($textOrHtml, "<b></b><i></i>");
$words = preg_split("/\s+/", $text);

return count($words);
}
?>


So in your code you can use this:
<?php $word1 = $common_informationRecord['words'] ?>
<?PHP echo maxWords($record['description'], $word1); ?>
<?php if (wordCount($record['description']) > $word1) : ?>...<a href="<?php echo $record['_link']; ?>">Read More</a><?php endif ?>


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] Hiding "...read more" when using maxWords

Thanks Jason,

Your bit of coding magic worked as planned and is now happily living in a CMSB Cookbook recipe for others to learn from.

Jerry Kornbluth
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php