http://www.perlmonks.org?node_id=375488


in reply to In search of a better way to trim string

This snippet uses a pure regexp approach to breaking a string up into stringlets of no more than 'n' length, broken on the first whitespace before 'n', OR a hard break at 'n' characters if there is no preceeding whitespace on which to break. Not sure if this is what you're after, but it seems to be a start in the right direction. If you simply want to crop at 'n' or less (depending on whitespace), remove the /g modifier and capture only the first stringlet.

use strict; use warnings; while ( my $string = <DATA> ) { chomp $string; my ( @stringlets ) = $string =~ m/(?:.{0,40}(?:\s|$))|(?:\S{40})/gs; print "$_\n" foreach @stringlets; print "\n\n"; } __DATA__ Now is the time for all good men to come to the aid of their country. Nowisthetimeforallgoodmentocometotheaidoftheircountry.

Applying the "..." elipses is trivial at this point.

I hope this helps... it's late, I may have misread the question. ;)


Dave