use strict; use warnings; use HTML::Truncate; my $snippet = <<""; First a technological update - The code that drives this site is avail +able for free on <a href="https://github.com/amacks/vatican_mss">GitH +ub</a>. I've just merged in a rather complex change to create proper + shelfmark sorting, fixing things like numbers-stored-as-strings and +handling roman numerals. Two problems yet unfixed are Fonds <strong> +P.I.O</strong>, with the middle "I" reading as a roman numeral, and < +strong>Arch.Cap.S.Pietro</strong> where sub-set "I" is read as roman +1 and everything gets confused.</p> my $ht = HTML::Truncate->new(); $ht->chars(100); print $ht->truncate($snippet), $/; __END__ First a technological update - The code that drives this site is avail +able for free on <a href="https://github.com/amacks/vatican_mss">GitH +ub</a>. I've&#8230;

HTML::Truncate. Long time since I used this for anything but it helped me out with similar needs a long time ago. There are lots of low level tools to do this kind of thing but you end up having to do a lot of #text character counting and such.

And that’s why this one breaks much later than your raw substr; it’s only counting displayed characters, not HTML content.

Update: if we correct your omission of the opening paragraph tag in the input, this is the output–

<p>First a technological update - The code that drives this site is av +ailable for free on <a href="https://github.com/amacks/vatican_mss">G +itHub</a>. I've&#8230;</p>

In reply to Re: Truncate HTML String by Your Mother
in thread Truncate HTML String by amacks

Title:
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, details, 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, summary, 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.