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

前端 未结 9 995
太阳男子
太阳男子 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:29

    Just want to share some alternative I found on the net.

    Using mb_str_split, you can use join to combine the words with
    .

    ';
    
        echo join('
    ', mb_str_split($text, 20));

    And finally create your own helper, perhaps mb_textwrap

    ') 
        {
            return join($concat, mb_str_split($text, $length));
        }
    }
    
    $text = '';
    // so simply call
    echo mb_textwrap($text);
    

    See screenshot demo:

提交回复
热议问题