Say I have a 200 character string that contains HTML markup. I want to show a preview of just the first 50 chars. without 'splitting up' the tags. In other words, the fragment should not contain a <b>
without a </b>
. Any server side processing should be in PHP.
views:
118answers:
3
A:
A simple approach might be to strip_tags()
first and then capture the excerpt.
Alix Axel
2010-01-20 21:47:30
I agree - this is what ive always done. the only downside is you lose the formatting for strong, em, etc.. as well as any links - but ive just always accepted it and moved on :-)
prodigitalson
2010-01-20 21:49:31
I'd prefer to have the tags...
George Edison
2010-01-20 22:14:17
+2
A:
You should check out Tidy HTML. Just cut it after the first 50 non-HTML characters, then run it through Tidy to fix the HTML.
tixxit
2010-01-20 21:52:39
A:
Short answer: convert it to DOM with DOMDocument::loadHTML($string)
then walk the tree counting the characters in the text nodes. When you hit your limit, replace the rest of that node with '...' or the empty string, and simply call $node->parentNode->removeChild($node)
on all subsequent nodes.
Nik M
2010-01-20 23:57:54