I am using Codeigniter’s character_limiter() function to truncate a string to a certain number of characters. I am using this to create post excerpts.
The problem is, the string contains HTML tags, so those characters are being counted, but they aren’t visible. Furthermore, it can cut the string off in the middle of a tag and mess up the page formatting.
For example, if I have the following:
This is some text with a <a href="http://google.com">a link</a>
If I limit it to 54 characters, it will cut off after the a in “a link” and there won’t be a closing tag and it turns everything after it into the anchor text.
How can I prevent this? Should I just strip out all HTML tags altogether before limiting characters?
Yes,
character_limiter(strip_tags($text),54);should work for you.