Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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)

In reply to Re: Clipping text in the middle of an open HTML tag by graff
in thread 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 chanting in the Monastery: (6)
    As of 2020-08-15 08:20 GMT
    Find Nodes?
      Voting Booth?
      Which rocket would you take to Mars?

      Results (78 votes). Check out past polls.