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 碼