Copy CodeThe code is as follows:
/* UTF-8 Chinese character truncation program */
$STR = "123 This is the test string";
$str 1 = "() ()";
Echo subutf8str ($str, 0, 3). "
";
Echo subutf8str ($str, 0,4). "
";
Echo subutf8str ($str 1,0,4). "
";
Echo subutf8str ($str 1,0,10). "
";
function Subutf8str ($str, $start =0, $length =80) {
$cur _len = 0; People understand the length of the string
$all _len = strlen ($STR); Machine Understanding String Length
if ($length > $all _len)
{
return $str;
}
for ($i = 0; $i < $all _len;)
{
if ($cur _len = = $start)
{
Break
}
if (Ord ($str [$i]) > 127)
{
$i + = 3;
}else{
$i + = 1;
}
$cur _len + +;
}
$start _pos = $i;
$temp _pos = $cur _len;
for (; $cur _len-$temp _pos < $length;)
{
if ($i >= $all _len)
Break
if (Ord ($str [$i]) > 127)
{
$i + = 3;
}else{
$i + = 1;
}
$cur _len + +;
}
$end _pos = $i;
Return substr ($str, $start _pos, $end _pos);
}
?>
In fact, PHP is originally born with many CharSet under the character interception scheme, the amount, so this is the way ... It's embarrassing.
Multibyte String functions function family,
String Mb_substr (string $str, int $start [, int $length [, String $encoding]]) is used to intercept strings
int Mb_strlen (String $str [, String $encoding]) returns the string length
....
Please see the PHP manual for details.
http://www.bkjia.com/PHPjc/326024.html www.bkjia.com true http://www.bkjia.com/PHPjc/326024.html techarticle Copy the code as follows:? PHP/* UTF-8 Chinese character Truncation program */$str = "123 This is the test string"; $str 1 = "() ()"; Echo subutf8str ($str, 0, 3). " BR "; Echo subutf8str ($str, 0,4) ...