Created
February 14, 2020 07:54
-
-
Save ynishi2014/5a1809d126273898e2a1e6e9afc0f077 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 is_utf8($str) | |
{ | |
$len = strlen($str); | |
for ($i = 0; $i < $len; $i++) { | |
$c = ord($str[$i]); | |
if ($c > 128) { | |
if (($c > 247)) { | |
return false; | |
} elseif ($c > 239) { | |
$bytes = 4; | |
} elseif ($c > 223) { | |
$bytes = 3; | |
} elseif ($c > 191) { | |
$bytes = 2; | |
} else { | |
return false; | |
} | |
if (($i + $bytes) > $len) { | |
return false; | |
} | |
while ($bytes > 1) { | |
$i++; | |
$b = ord($str[$i]); | |
if ($b < 128 || $b > 191) { | |
return false; | |
} | |
$bytes--; | |
} | |
} | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment