Blog Entries with different lengths of info

3 posts by 2 authors in: Forums > CMS Builder
Last Post: December 6, 2012   (RSS)

Re: [crazyfish] Blog Entries with different lengths of info

By gregThomas - December 6, 2012

Hi,

I've done some testing, and this should do the trick:

<?php if(str_word_count($record['content']) > 25): ?>
<?php echo implode(' ', array_slice(str_word_count($record['content'], 1),0,25)); ?><br>
<a href="<?php echo $record['_link'] ?>" style="color: #008000"><?php echo $record['_link'] ?></a><br/><br/>
<?php else: ?>
<?php echo $record['content']; ?><br>
<?php endif; ?>


The PHP function str_word_count will give you the number of words in a string. If it's greater than 25 words, then display the first 25 words of the string, using str_word_count function to break the string into an array of words, then using array slice to select the first 25 words, and gluing the array back into a string with the implode function.

If the $record['content'] is a wysiwyg editor field, it might be worth using the php function strip_tags (http://php.net/manual/en/function.strip-tags.php). To remove any images and other tags, as these will be included in the word count, and you could end up with an opening tag being included in the first 25 words, but not a closing tag.

Let me know if you have any questions.

Thanks!
Greg Thomas







PHP Programmer - interactivetools.com

Re: [greg] Blog Entries with different lengths of info

By crazyfish - December 6, 2012

Awesome. The problem was it is indeed a WYSIWYG editor and there were links, paragraphs etc in the section. So I took your code and changed it a bit to work for guys like me!

<?php if(str_word_count($record['content']) > 100): ?>
<?php echo maxWords($record['content'], 50); ?>...<br />
<a href="<?php echo $record['_link'] ?>">[ Read More ]</a><br/>
<?php else: ?>
<?php echo $record['content']; ?><br>
<?php endif; ?>


Thank you for your quick response and excellent help.