in reply to Re^3: In search of a better way to trim string length
in thread In search of a better way to trim string
This is cool but it suffers from a problem that the first one I wrote did too. Give it:
my $mouse = 'This is a story about mice, "I dint know mice were so sma +rt."'; print trimTo($mouse,30), "\n";
And you can get back things like: This is a story about mice,... The comma (or semi-color, period, quote, etc) is a bit jarring.
This is a recent stab I've ended up using but I would love to see other ideas/answers/hybrids:
sub chop_to_size { my ($text, $length) = @_; return $text if length $text < $length; # make room for ellipsis my $chop = $length - 3; $text =~ s/^\s*(.{$chop})\s*.+$/$1/; $text =~ s/[\s[:punct:]]+$//; $text .= "..."; $text; }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^5: In search of a better way to trim string length
by dragonchild (Archbishop) on Jul 19, 2004 at 19:37 UTC | |
Re^5: In search of a better way to trim string length
by BrowserUk (Patriarch) on Jul 19, 2004 at 20:12 UTC |
In Section
Seekers of Perl Wisdom