http://qs321.pair.com?node_id=641063

hacker has asked for the wisdom of the Perl Monks concerning the following question:

I seem to recall the CB had this problem a few years ago, and it was solved... and now I'm running into it.

Here's the situation:

I have a database of news articles, which I query for 3 random entries. I clip the article summary at 200 characters and display the result. I'm using MySQL to clip the articles, like so:

$news_sth = $dbh->prepare (qq{ SELECT article_id, article_title, LEFT(article_body, 220) AS CLIP, DATE_FORMAT(article_date, '%W %M %D %Y') as my_date from news_articles group by article_id order by RAND() LIMIT 3});

This part works great... until 200 characters happens to fall right in the middle of an open HTML tag.

This is some random HTML from one of my <em>news articles that would be clipped...

Note in the above example, I've clipped after the opening <em> tag, but not after the matched closing tag.

What I'm trying to figure out, is...

  1. Should I be clipping with MySQL? Or substr() in Perl?
  2. Is there a way to "rewind" to the last open tag, so I can close it at the end of my clipped section?
  3. Another method?

The problem is most obvious when I'm clipping in the middle of a blockquote section or an ordered/unordered list. When the next article is drawn, it inherits the formatting left over from the previous open tag.

Yuck.

How should I best approach a fix for this?

Replies are listed 'Best First'.
Re: Clipping text in the middle of an open HTML tag
by graff (Chancellor) on Sep 26, 2007 at 01:34 UTC
    If your news articles tend to be really big, such that fetching the whole story text every time would be a hindrance for your application, you might want to consider how important it really is to try to preserve html markup in an initial 200 characters for your "sample" display.

    In effect, I think your choices are:

    1. Select the whole text for each story, run it through HTML::TokeParser or equivalent, keeping track of (relevant) tag nesting as you march along, until you hit your 200-character limit, and close all open tags at that point.

    2. Select "LEFT(article_body,280)", use s/<[^>]+>//g to strip out all html tags, then truncate the result at 200 characters (or at a word boundary close to that length)

    Either way, you would be counting only the displayable characters (although in the first approach, with most html formatting preserved, the display space taken up by the 200 characters would vary with the formatting). Of course, if there are lots of character entity references in the text, you probably want a way to count each as "one character"...

    So think about the desired output: what really makes the most sense, in your app, for that "sample" display? Maybe you want to do stuff like replacing certain tags with particular punctuation (e.g. "ul" and "ol" with emdash, "li" with asterisk, "hN" with underscore, "p" with "/", or whatever). That way, the formatting counts for something in the final result, but in a controlled and consistent way.

    (updated to fix grammar)

Re: Clipping text in the middle of an open HTML tag
by Your Mother (Archbishop) on Sep 26, 2007 at 01:00 UTC
Re: Clipping text in the middle of an open HTML tag
by Gangabass (Vicar) on Sep 26, 2007 at 00:36 UTC

    I think you need Perl solution. First you create list of tags that needs closing tag. Second you don't clip until current open tag is closed or you may clip text and close tag by yourself. Also there is situation when you need to close more than one tags (for example li+ul).

Re: Clipping text in the middle of an open HTML tag
by skx (Parson) on Sep 26, 2007 at 19:21 UTC