+ add isUTF8 function. task#646.
This commit is contained in:
@@ -233,6 +233,47 @@ class helper
|
||||
return base64_decode(strtr($string, '.', '/'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Judge a string is utf-8 or not.
|
||||
*
|
||||
* @param string $string
|
||||
* @author hmdker@gmail.com
|
||||
* @see http://php.net/manual/en/function.mb-detect-encoding.php
|
||||
* @static
|
||||
* @access public
|
||||
* @return bool
|
||||
*/
|
||||
static public function isUTF8($string)
|
||||
{
|
||||
$c = 0;
|
||||
$b = 0;
|
||||
$bits = 0;
|
||||
$len = strlen($string);
|
||||
for($i=0; $i<$len; $i++)
|
||||
{
|
||||
$c = ord($str[$i]);
|
||||
if($c > 128)
|
||||
{
|
||||
if(($c >= 254)) return false;
|
||||
elseif($c >= 252) $bits=6;
|
||||
elseif($c >= 248) $bits=5;
|
||||
elseif($c >= 240) $bits=4;
|
||||
elseif($c >= 224) $bits=3;
|
||||
elseif($c >= 192) $bits=2;
|
||||
else return false;
|
||||
if(($i+$bits) > $len) return false;
|
||||
while($bits > 1)
|
||||
{
|
||||
$i++;
|
||||
$b=ord($str[$i]);
|
||||
if($b < 128 || $b > 191) return false;
|
||||
$bits--;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute the diff days of two date.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user