Hiding "...read more" when using maxWords

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

Hi All,

When using the MaxWords function and there are less words than the maxWords value, I thought it would be easy to use an if statement to hide the ... (read more) link, but it seems to be harder than I thought. I just don’t know why.

Here’s the usual standard maxWords 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;
}
?>


And here’s what I thought would work (with $words defined in the function and, $word1 defined below) but I get an undefined error for the $words variable
<?php $word1 = $common_informationRecord['words'] ?>
<!– show the values of the 2 variables –>
<br />Word1 <?PHP echo $word1 ; ?><br />
<br />Words <?PHP echo $words ; ?><br />
<!– show the text –>
<?PHP echo maxWords($record['description'], $word1);
?><?php if ($words < $word1) : ?>...<a href="<?php echo $record['_link']; ?>">Read More</a><?php else :?> ...There are no more words to display<?php endif ?>


Thanks for any ideas,

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

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