The others gave solutions with unnecessarily complicated loops. To get the exact same functionality is as simple as the following:
#!/usr/bin/perl -w use strict; use HTML::TokeParser::Simple; my $total = 0; my $p = HTML::TokeParser::Simple->new(*DATA); my $length_left = shift || 40; my $abstract = ""; while (my $t = $p->get_token) { $_ = $t->as_is; if ($t->is_text) { s/\s+/ /g; s/^(.{1,$length_left}\S*).*/$1/s; $length_left -= length; } $abstract .= $_; last unless $length_left > 0; } print $abstract, $/;
However, this is broken: it will leave unbalanced open tags. A first step to fix this is to move the last inside the if so that any trailing closing tags right after the last piece of counted plaintext will be accepted. However, that can still leave us with unbalanced tags. So we need to have a stack:
#!/usr/bin/perl -w use strict; use HTML::TokeParser::Simple; my $total = 0; my $p = HTML::TokeParser::Simple->new(*DATA); my $length_left = shift || 40; my $abstract = ""; my @stack; while (my $t = $p->get_token) { $_ = $t->as_is; if ($t->is_text) { s/\s+/ /g; s/^(.{1,$length_left}\S*).*/$1/s; $length_left -= length; } elsif($t->is_start_tag) { push @stack, $t->return_tag; } elsif($t->is_end_tag) { pop @stack if $stack[-1] eq $t->return_tag; } $abstract .= $_; last unless $length_left > 0; } $abstract .= join '', map "</$_>", reverse @stack; print $abstract, $/;
Note this only takes minimal provisions for dealing with HTML with invalidly nested tags. The way it is, it may produce extraneous closing tags, which is usually the less harmful alternative. Except for this corner case, it does exactly what you need. You can bulletproof it against those cases if you spend significantly more time on the elsif($t->is_end_tag) branch.

Makeshifts last the longest.


In reply to Re: Extracting a substring of N chars ignoring embedded HTML by Aristotle
in thread Extracting a substring of N chars ignoring embedded HTML by FamousLongAgo

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.