3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function cn_substr_utf8($str, $length, $start=0) { if(strlen($str) < $start+1) { return ''; } preg_match_all("/./su", $str, $ar); var_dump($ar); $str = ''; $tstr = ''; //为了兼容mysql教程4.1以下版本,与数据库教程varchar一致,这里使用按字节截取 for($i=0; isset($ar[0][$i]); $i++) { if(strlen($tstr) < $start) { $tstr .= $ar[0][$i]; } else { if(strlen($str) < $length + strlen($ar[0][$i]) ) { $str .= $ar[0][$i]; } else { break; } } } return $str; } $a = '测试这是一个测试'; $a = 'xxxxbbbbbnnn'; $b = cn_substr_utf8($a,5,0); var_dump($b); ?>
Output for 4.3.0 - 5.6.28, hhvm-3.10.0 - 3.13.2, 7.0.0 - 7.1.0
array(1) { [0]=> array(12) { [0]=> string(1) "x" [1]=> string(1) "x" [2]=> string(1) "x" [3]=> string(1) "x" [4]=> string(1) "b" [5]=> string(1) "b" [6]=> string(1) "b" [7]=> string(1) "b" [8]=> string(1) "b" [9]=> string(1) "n" [10]=> string(1) "n" [11]=> string(1) "n" } } string(6) "xxxxbb"