MW

Tag wordwrap

Recent Posts

UTF-8 Wordwrap (May 05, 2007)

If you use UTF-8 in your PHP projects you may want to use [wordwrap](http://www.php.net/wordwrap)(). But that function can’t handle multibyte characters and may mess up your text.

Don’t be annoyed - help is near!

The only PHP UTF-8 wordwrap function I found was the one by tjomi4 at yeap dot lv in the notes of the PHP manual. I took it and improved it a bit:

  1. completly the same syntax as the original wordwrap function: string utf8_wordwrap(string $str, integer $width, string $break [, bool $cut]);
  2. The $cut parameter is supported (tjomi4’s function only supports $cut = true).
    But be careful : I use regular expression word boundaries (\b) for this feature. I’m not sure if this works everywhere!

  3. The function uses the multibyte extension if installed for counting the string length
  4. The regular expression inside the while loop is shorter and uses [preg_match](http://www.php.net/preg_match)() instead of [preg_replace](http://www.php.net/preg_replace)(). That should improve performance and prevent a strange bug (Compilation failed: regular expression too large)

continue reading...