in reply to Re: How do I truncate a string while preserving words?
in thread How do I truncate a string while preserving words?
return substr($string, 0, rindex($string, ' ') - 1);I'd cut it on any whitespace, or at least, in front of the last partial word, like this:
I'm assuming that a hyphen is a part of a word. Your idea may differ from mine.$string =~ s/\s*[\w\-]*$/.../; return $string;
p.s. Hmm, that'll leave any trailing nonword/nonspace characters, for example punctuation characters, intact, making the string just one character too long.
Perhaps this is better?
or$string =~ s/\s*(?:[\w\-]+|\W)$/.../;
$string =~ s/\s*[\w\-]+$/.../ or $string =~ s/\s*\W$/.../;
|
|---|