Smarter word-wrap in PHP for long words?

前端 未结 5 1486
無奈伤痛
無奈伤痛 2020-12-05 18:53

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

5条回答
  •  感情败类
    2020-12-05 19:36

    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

提交回复
热议问题