Skip to content

Instantly share code, notes, and snippets.

@ynishi2014
Created February 14, 2020 07:54
Show Gist options
  • Save ynishi2014/5a1809d126273898e2a1e6e9afc0f077 to your computer and use it in GitHub Desktop.
Save ynishi2014/5a1809d126273898e2a1e6e9afc0f077 to your computer and use it in GitHub Desktop.
<?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