Multi-byte safe wordwrap() function for UTF-8

前端 未结 9 1011
太阳男子
太阳男子 2020-12-01 13:17

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 di

9条回答
  •  囚心锁ツ
    2020-12-01 13:41

    Here is the multibyte wordwrap function i have coded taking inspiration from of others found on the internet.

    function mb_wordwrap($long_str, $width = 75, $break = "\n", $cut = false) {
        $long_str = html_entity_decode($long_str, ENT_COMPAT, 'UTF-8');
        $width -= mb_strlen($break);
        if ($cut) {
            $short_str = mb_substr($long_str, 0, $width);
            $short_str = trim($short_str);
        }
        else {
            $short_str = preg_replace('/^(.{1,'.$width.'})(?:\s.*|$)/', '$1', $long_str);
            if (mb_strlen($short_str) > $width) {
                $short_str = mb_substr($short_str, 0, $width);
            }
        }
        if (mb_strlen($long_str) != mb_strlen($short_str)) {
            $short_str .= $break;
        }
        return $short_str;
    }
    

    Dont' forget to configure PHP for using UTF-8 with :

    ini_set('default_charset', 'UTF-8');
    mb_internal_encoding('UTF-8');
    mb_regex_encoding('UTF-8');
    

    I hope this will help. Guillaume

提交回复
热议问题