my $str = "mom went to the food market today and got me some milk"; my $maxl = 18; my $show; foreach (split(/\s+/, $str)) { if (length("$show $_")+3 >= $maxl) { $show .= '...' and last } else { $show .= " $_" } } print substr($show, 1),"\n";