http://www.perlmonks.org?node_id=152558


in reply to Truncating HTML early

Thanks, all for your excellent suggestions.

While working on this today, I had yet another idea, so I thought I'd share it here.

later edit by nop:
the following suggestion was not a good one --
leaving in empty tags creates HTML littered with empty bulleted lists, etc.
yuck.
my final solution was to stop taking text after some limit of words, then use tidy to clean out the empty tags.

original post:
Assuming there's much text relative to markup, and assuming the markup is reasonably well-formed, an easy solution to this problem using HTML::Parser is count words in the text handler, and when that count exceeds a limit, have the handler stop appending new text to the result string. Allow the start and end handlers to continue adding their tags. The idea here is that when the text limit is reached, the rest of the markup (probably not much) will flow out empty.

This wouldn't be a good solution for html with heavy markup, but I think it may work in my case.......

nop