Skip to content

Instantly share code, notes, and snippets.

@josephdicdican
Last active September 12, 2018 03:32
Show Gist options
  • Save josephdicdican/cff3f1552e8bd647377b381574e5a913 to your computer and use it in GitHub Desktop.
Save josephdicdican/cff3f1552e8bd647377b381574e5a913 to your computer and use it in GitHub Desktop.
mb_textwrap - wrapping multibyte characters
<?php
if ( !function_exists('mb_str_split') ) {
function mb_str_split($string, $split_length = 1) {
mb_internal_encoding('UTF-8');
mb_regex_encoding('UTF-8');
$split_length = ($split_length <= 0) ? 1 : $split_length;
$mb_strlen = mb_strlen($string, 'utf-8');
$array = array();
for($i = 0; $i < $mb_strlen; $i += $split_length) {
$array[] = mb_substr($string, $i, $split_length);
}
return $array;
}
}
if( !function_exists('mb_textwrap') ) {
function mb_textwrap($text, $length = 20, $concat = '<br>') {
return join($concat, mb_str_split($text, $length));
}
}
$text = "快速的棕色狐狸跳过懒狗。快速的棕色狐狸跳过懒狗。快速的棕色狐狸跳过懒狗。快速的棕色狐狸跳过懒狗。";
echo "Input: ". $text . "<br>";
echo "Output: <br>";
echo mb_textwrap($text);
/*
Input: 快速的棕色狐狸跳过懒狗。快速的棕色狐狸跳过懒狗。快速的棕色狐狸跳过懒狗。快速的棕色狐狸跳过懒狗。
Output:
快速的棕色狐狸跳过懒狗。快速的棕色狐狸跳
过懒狗。快速的棕色狐狸跳过懒狗。快速的棕
色狐狸跳过懒狗
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment