Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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.


How should I best approach a fix for this?

In reply to Clipping text in the middle of an open HTML tag by hacker

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (5)
    As of 2020-08-15 07:20 GMT
    Find Nodes?
      Voting Booth?
      Which rocket would you take to Mars?

      Results (78 votes). Check out past polls.