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
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
Thanks for any ideas,
Jerry Kornbluth
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
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
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:
So in your code you can use this:
Hope this helps
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/
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
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
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php