my @chars = split //, $full_text; my @excerpt; my $in_tag = 0; my $limit=200; # Get exactly $limit characters of text while ( my $char = shift @chars ) { push @excerpt, $char; $in_tag++ if $char eq '<'; $in_tag-- if $char eq '>'; $count++ unless $in_tag; last if $count > $limit and !$in_tag; } #Now make sure we get the last word until boundary $in_tag = 0; # just in case while ( my $char = shift @chars ){ $in_tag++ if $char eq '<'; $in_tag-- if $char eq '>'; last if $char =~/\s/ and !$in_tag; push @excerpt, $char; } my $excerpt = join ('', @excerpt);