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 is also a solution (for browsers etc.):
$string = 'hello! heeeeeeeeeeeeeeeeeeeeeereisaverylongword';
echo preg_replace('/([^\s]{20})(?=[^\s])/', '$1'.'', $string);
It puts a at words with 20 or more characters
means "word break opportunity" so it only breaks if it has to (dictated by width of element/browser/viewer/other). It's invisible otherwise.
Good for fluid/responsive layout where there is no fixed width. And does not wrap odd like php's wordwrap