Skip to content

Instantly share code, notes, and snippets.

@mly520
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mly520/f0d10f4c7a1b541a75b0 to your computer and use it in GitHub Desktop.
Save mly520/f0d10f4c7a1b541a75b0 to your computer and use it in GitHub Desktop.
<?php
function mb_wordwrap($string, $width=75, $break="\n", $cut = false) {
if (!$cut) {
$regexp = '#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){'.$width.',}\b#U';
} else {
$regexp = '#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){'.$width.'}#';
}
$string_length = mb_strlen($string,'UTF-8');
$cut_length = ceil($string_length / $width);
$i = 1;
$return = '';
while ($i < $cut_length) {
preg_match($regexp, $string,$matches);
$new_string = $matches[0];
$return .= $new_string.$break;
$string = substr($string, strlen($new_string));
$i++;
}
return $return.$string;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment