I have some content (some from an external source, some specially written) that is displayed in a kind of blog format. I want to cut off the text after a certain number of characters, so currently I’m doing this:
<?=substr( strip_tags($a['content']), 0, 400 )?>
The problem is, this loses all the formatting and I just get one massive blob of text. But if I don’t strip the tags, obviously some tags will go unclosed and mess up the layout.
What would be a good way to truncate after X number of characters, but also keep some basic formatting?
Not convinced if this is the best approach for your problem, but it is a simple one, and would work if you’re only concerned about preserving 1 or 2 tags to retain some basic formatting in the html.
So, you could do something like: find the tags you want to keep and replace them with some unique combination of characters, then after truncating the string, find/replace the unique string combination you created with the tag you originally replaced them with.