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="">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

2 answers

Yes, character_limiter(strip_tags($text),54);

should work for you.



echo word_limiter(strip_tags($description),40);


What I do for the meta description, try



All Articles