1 <?php 2 function myMbSubstr($str, $start, $length, $charset){ 3 $charsets["utf-8"] = $charsets["utf8"] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/"; 4 $charsets["gb2312"] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/"; 5 $charsets["gbk"] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/"; 6 $charsets["big5"] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/"; 7 8 preg_match_all($charsets[$charset], $str, $matches); #按照指定编码将字符串切分成一个数组 9 10 #var_dump($matches); 11 12 $substr = implode("", array_slice($matches[0], $start, $length)); #获取数组的子数组,并将子数组的元素连接起来 13 14 return $substr; 15 } 16 17 $str = "I'm a 码农"; 18 19 echo "<br>" . myMbSubstr($str, 0, 7, "utf8") . "<br>"; 20 echo mb_substr($str, 0, 7, "utf8"); 21 ?>
输出
array(1) { [0]=> array(8) { [0]=> string(1) "I" [1]=> string(1) "'" [2]=> string(1) "m" [3]=> string(1) " " [4]=> string(1) "a" [5]=> string(1) " " [6]=> string(3) "码" [7]=> string(3) "农" } }
I'm a 码
I'm a 码