Directly on code:
/** * @param $str * @param $start * @param $length * * @return string/public stat
IC function Usubstr ($str, $start, $length = null) {//normal intercept first.
$res = substr ($str, $start, $length);
$strlen = strlen ($STR); /* Then determine whether the 6 bytes are complete (not broken)////If the parameter start is a positive number if ($start >= 0) {//forward and intercept about 6 bytes $next _start = $start + $ Length Initial position $next _len = $next _start + 6 <= $strlen?
6: $strlen-$next _start;
$next _segm = substr ($str, $next _start, $next _len); If the 1th byte is not the first byte of the full character, then intercept about 6 bytes $prev _start = $start-6 > 0?
$start-6:0;
$prev _segm = substr ($str, $prev _start, $start-$prev _start); //Start is a negative number else {//forward intercept approximately 6 bytes $next _start = $strlen + $start + $length;//initial position $next _len = $ne Xt_start + 6 <= $strlen?
6: $strlen-$next _start;
$next _segm = substr ($str, $next _start, $next _len);
If the 1th byte is not the first byte of the full character, then intercept about 6 bytes. $start = $strlen + $start; $prev _start = $start-6 > 0?
$start-6:0;
$prev _segm = substr ($str, $prev _start, $start-$prev _start); //Determine if the first 6 bytes conform to the UTF8 rule if (preg_match (' @^ ([x80-xbf]{0,5}) [xc0-xfd]?@ ', $next _segm, $bytes)) {if (!empty ($byt
Es[1]) {$bytes = $bytes [1];
$res. = $bytes;
}//Determine if 6 bytes are compliant with the UTF8 rule $ord 0 = ord ($res [0]);
if (128 <= $ord 0 && 191 >= $ord 0) {//intercept later and add to the front of Res. if (Preg_match (' @[xc0-xfd][x80-xbf]{0,5}$@ ', $prev _segm, $bytes)) {if (!empty ($bytes [0])) {$bytes = $b
Ytes[0]; $res = $bytes.
$res; }} if (Strlen ($res) < $strlen) {$res = $res.
'...';
return $res; }