If you use the character limiting function that’s exactly what you’ll get, and words can be cut off. But if you want to limit the word count, Dave Edis from Interactive Tools shares his usual uncanny wisdom:
Insert this function at the top of your page, or before you want to invoke the word limiting function:
<?PHP function maxWords($textOrHtml, $maxWords) { $text = strip_tags($textOrHtml); $words = preg_split("/\s+/", $text, $maxWords+1); if (count($words) > $maxWords) { unset($words[$maxWords]); } $output = join(' ', $words); return $output; } ?>
Then put this code where you want the words limited (to 5 words):
<?PHP echo maxWords($record['content'], 5); ?> If you want to add ...more and a link, use something like this:
<?PHP echo maxWords($record['content'], 5); ?>...<a href="<?php echo $record['_link']; ?>”>Read More</a>
or
<?PHP echo maxWords($record['content'], 5); ?>...<a class="read-more" href="http://www.your_site.com/your_detail_page.php?<?php echo $record['num'] ?>">(Read More)</a>
To allow certain tags like or <p> to appear in your text, change the strip_tags code:
$text = strip_tags($textOrHtml);
to
$text = strip_tags($textOrHtml, '<br />');
The function is pretty literal, but it seems to automatically include the closing tag, so include all the flavors of all the tags (spaces and slashes, slashes with no space, etc.) that you want to include. Like this:
$text = strip_tags($textOrHtml, '<br /><p>');
The strip_tags function is optional and is used to insure that extra tags, spaces, etc are not counted in the word count. If this is not a great concern (like if you're using text box fields, you can eliminate the line of code entirely.
WYSIWYG EDITORS If you’re using a WYSIWYG Editor and are losing some formatting when using the MaxWord function, Jason Sauchuk of Interactive Tools suggests using this version of the function instead:
<?PHP function maxWords($textOrHtml, $maxWords) { $text=str_replace("<p>","*P*",$textOrHtml); $text= str_replace("</p>","*/P*",$text); $text = strip_tags($text); $words = preg_split("/\s+/", $text, $maxWords+1); if (count($words) > $maxWords) { unset($words[$maxWords]); } $output = join(' ', $words); $output=str_replace("*P*","<p>",$output); $output=str_replace("*/P*","</p>",$output); $output.="</p>"; return $output; } ?>
If you want to display an alternate link text, or nothing if the field contains less words then you've specified, according to Jason, you'll first have get the actual number of words from your original string.
Here' a function he offered that goes through the same process as maxWords, but just returns the count. You'd add it to your code in addition to the maxWords function:
<?PHP function wordCount($textOrHtml) { $text = strip_tags($textOrHtml, "<b></b><i></i>"); $words = preg_split("/\s+/", $text); return count($words); } ?>
Then in your body code you can use this: (Since my client wanted to control the amount of words allowed in the maxWords call, I added a field called "words" in a single record editor called "common_information" and defined the variable $word1 to reflect that value )
<?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 ?>
You can also add alternative text or links using an <?php else :?> like this:
<?php if (wordCount($record['description']) > $word1) : ?>...<a href="<?php echo $record['_link']; ?>">Read More</a><?php else :?> <a href="<?php echo $record['_link']; ?>">Your Text Here</a><?php endif ?>
|