Shrink the string to a specific number of characters, ignoring HTML
I am using Codeigniter's character_limiter () function to trim a string to a specific number of characters. I am using this to create excerpt from extracts.
The problem is that the string contains HTML tags, so these characters are counted but not visible. Also, it can cut a line in the middle of the 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 gets disabled after the a in the "link", and there is no end tag, and it turns everything after it into anchor text.
How can I prevent this? Should I just strip out all HTML tags entirely before limiting characters?
source to share