Skip to content

Instantly share code, notes, and snippets.

@tanakahisateru
Created December 9, 2014 11:59
Show Gist options
  • Save tanakahisateru/f0549e7ec3ae7c870912 to your computer and use it in GitHub Desktop.
Save tanakahisateru/f0549e7ec3ae7c870912 to your computer and use it in GitHub Desktop.
Remove UTF-8 bad bytes
<?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