Codeigniter String Helper

One of the great strengths of Codeigniter is the Helper Libraries and one which I use quite lot is the Text Helper, in particular the word and character limiter function.

Recently whilst working on a project I created a river of news type page, pretty much the same as you would find on Drupal.

However I kept seeing some very strange behaviours, the style would break a different points.

After much head banging and hair pulling I worked out the problem. This was one of those “Can’t see the trees for the wood”.

Some of the text that was being feed into the “River of News” was coming from logged in users that of course had access to the WYSIWYG editor. Here lies the problem the editor was of course inserting <p> tags.  When the codeigniter word limiter function ran and cut off the text at the required length it of course also cut off the ending </p> tag.

Simple I know but it took me hours to debug that.

so here is the correct way

foreach ($posts as $post) {

$string = strip_tags($post->body); //$post being the array from the controller

print word_limiter($string, 50);

}

Happy Coding

This entry was posted in News and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>