PHP's wordwrap()
function doesn't work correctly for multi-byte strings like UTF-8.
There are a few examples of mb safe functions in the comments, but with some different test data they all seem to have some problems.
The function should take the exact same parameters as wordwrap()
.
Specifically be sure it works to:
- cut mid-word if
$cut = true
, don't cut mid-word otherwise - not insert extra spaces in words if
$break = ' '
- also work for
$break = "\n"
- work for ASCII, and all valid UTF-8
Total time: 0.0020880699 is good time :)
Just want to share some alternative I found on the net.
Using
mb_str_split
, you can usejoin
to combine the words with<br>
.And finally create your own helper, perhaps
mb_textwrap
See screenshot demo:
Because no answer was handling every use case, here is something that does. The code is based on Drupal’s
AbstractStringWrapper::wordWrap
.I haven't found any working code for me. Here is what I've written. For me it is working, thought it is probably not the fastest.
This one seems to work well...