/**
* Checks to see if a string is utf8 encoded.
*
* @author by bmorel at ssi dot fr
*
* @param string $Str The string to be checked
* @return bool True if $Str fits a UTF-8 model, false otherwise.
*/
public static function isUtf8($str)
{
$length = strlen($str);
for ($i = 0; $i < $length; $i++) {
if (ord($Str[$i]) < 0x80) {
continue;
}
elseif ((ord($str[$i]) & 0xE0) == 0xC0) {
$n = 1;
}
elseif ((ord($str[$i]) & 0xF0) == 0xE0) {
$n = 2;
}
elseif ((ord($str[$i]) & 0xF8) == 0xF0) {
$n = 3;
}
elseif ((ord($str[$i]) & 0xFC) == 0xF8) {
$n = 4;
}
elseif ((ord($str[$i]) & 0xFE) == 0xFC) {
$n = 5;
}
else {
return false;
}
for ($j = 0; $j < $n; $j++) {
if ((++$i == $length) || ((ord($str[$i]) & 0xC0) != 0x80)) {
return false;
}
}
}
return true;
}
是utf-8编码返回true,否则返回false