in reply to Extracting a substring of N chars ignoring embedded HTML

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.