tags:

views:

118

answers:

3

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.

A: 

A simple approach might be to strip_tags() first and then capture the excerpt.

Alix Axel
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
I'd prefer to have the tags...
George Edison
+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
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