sub trunc { my $max_words = shift or return ''; my $text = shift or return ''; $max_words--; $text =~ /(.+?(?:\s.+?){$max_words})\s+[^\s]/ and return "$1..."; return $text; }
The purpose is simple: you want to show just the first $n words of a text? trunc($n, $text) will do the trick...
But not perfectly!
Issues:
What I want is a standard text truncating function (by number of words or possible amount of characters) that I'd be able to use everywhere. There are probably other issues besides those two.
Anyone has personal code to do this to share? Or perhaps other issues with this code to point?
In reply to Truncating real text by cog
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |