Last active
August 29, 2015 14:07
-
-
Save mly520/f0d10f4c7a1b541a75b0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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