I’m looking for a way to make word-wrap in PHP a bit smarter. So it doesn’t pre-break long words leaving any prior small words alone on one line.
Let’s say I have this (the real text is always completely dynamic, this is just to show):
wordwrap('hello! heeeeeeeeeeeeeeereisaverylongword', 25, '<br />', true);
This outputs:
hello!
heeeeeeeeeeeeeeereisavery
longword
See, it leaves the small word alone on the first line.
How can I get it to ouput something more like this:
hello! heeeeeeeeeeee
eeereisaverylongword
So it utilizes any available space on each line. I have tried several custom functions, but none have been effective (or they had some drawbacks).
I’ve had a go at the custom function for this smart wordwrap:
EDIT: Spotted a couple of caveats. One major caveat with this (and also with the native function) is the lack of multibyte support.