Created
December 9, 2014 11:59
-
-
Save tanakahisateru/f0549e7ec3ae7c870912 to your computer and use it in GitHub Desktop.
Remove UTF-8 bad bytes
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 | |
$text = "あいうえお"; | |
$text1 = substr($text, 1); | |
$text2 = substr($text, 2); | |
echo mb_strimwidth($text1, 0, 10) . "\n"; | |
echo mb_strimwidth($text2, 0, 10) . "\n"; | |
function valid($text) { | |
return (ord($text[0]) & 0xa0) != 0x80; | |
} | |
function validate($text) { | |
while (!valid($text)) { | |
$text = substr($text, 1); | |
} | |
return $text; | |
} | |
echo mb_strimwidth(validate($text1), 0, 10) . "\n"; | |
echo mb_strimwidth(validate($text2), 0, 10) . "\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment