05 May 2009

Limit Characters or Words in ExpressionEngine

Web Development, ExpressionEngine posted by Jarrett Barnett

The more I use ExpressionEngine, the more I love it as there’s a solution for anything that I can find a need for.

I couldn’t find a parameter that I could use in ExpressionEngine (that was already built in) that limited how many characters were used from a variable. In PHP, you could use substr() and trim(), but I wanted to avoid using PHP in ExpressionEngine as much as possible. So after a bit of searching I found this ExpressionEngine plugin called “Character Limiter” which allows me to use the following expression:

{exp:char_limit total="100"}

Simple as that.

You may find that you would rather limit the number of words outputted. To do this, use the ExpressionEngine plugin called “Word Limit Plus“. Upload “pi.word_limit_plus.php” to the /system/plugins/ directory, and use the following expression to limit the amount of words outputted:

{exp:word_limit_plus if_exceeds="600" stop_after="500" the_link="<a href='{title_permalink=weblog/comments}'>MORE...</a>"}

I haven’t used this plugin yet, but from the “Word Limit Plus” plugin documentation, the “if_exceeds” parameter is the amount of words that need to be met before the text is triggered to be truncated. The amount of words that the text string would be truncated TO is the “stop_after” parameter. 

Again I haven’t confirmed this, but if I am correct, then a paragraph that has more than 600 words would be truncated to only be 500 words, while a paragraph of 599 words would not be truncated at all. I imagine this is to prevent from having blogs posts that truncate and leave out only a few words (obviously if there’s only a few more words left to display of a blog post, why not just display them rather than truncate the extra words out).

The “the_link” parameter is not required, but from what I can tell, its tacked onto the end of the outputted text and serves as a way to display a “Read more…” link at the end of a blog post.

A good side note is through my research of the “Word Limit Plus” ExpressionEngine plugin, I did find that because the “Word Limit Plus” looks for spaces (to determine the word count), consecutive spaces may cause an error.

None-the-less, the ExpressionEngine “Word Limit Plus” plugin could be useful for summarizing blog posts on your home page, building a custom RSS feed to be used for feeding data to Twitter, or simply feeding data to another portion of your site. I won’t go into too much detail about it, but I’m sure the possibilities are fairly endless.

Comments are disabled for this article.