3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function substr_utf8($string, $start, $length) { $chars = $string; $i=0; do { if (preg_match ("/[0-9a-zA-Z]/", $chars[$i])){ $m++; } else { $n++; }//非英文字节, $k = $n/3 + $m/2; $l = $n/3 + $m; $i++; } while ($k < $length); $str1 = mb_substr($string,$start,$l,'utf-8'); return $str1; } $a = '测试这是一个测试'; $a = 'xxxxbbbbbnnn'; $b = substr_utf8($a,0,5); var_dump($b); ?>
Output for 5.0.4 - 5.6.28, 7.0.0 - 7.1.0
Notice: Undefined variable: m in /in/Ptef2 on line 8 Notice: Undefined variable: n in /in/Ptef2 on line 12 Notice: Undefined variable: n in /in/Ptef2 on line 13 Notice: Undefined variable: n in /in/Ptef2 on line 12 Notice: Undefined variable: n in /in/Ptef2 on line 13 Notice: Undefined variable: n in /in/Ptef2 on line 12 Notice: Undefined variable: n in /in/Ptef2 on line 13 Notice: Undefined variable: n in /in/Ptef2 on line 12 Notice: Undefined variable: n in /in/Ptef2 on line 13 Notice: Undefined variable: n in /in/Ptef2 on line 12 Notice: Undefined variable: n in /in/Ptef2 on line 13 Notice: Undefined variable: n in /in/Ptef2 on line 12 Notice: Undefined variable: n in /in/Ptef2 on line 13 Notice: Undefined variable: n in /in/Ptef2 on line 12 Notice: Undefined variable: n in /in/Ptef2 on line 13 Notice: Undefined variable: n in /in/Ptef2 on line 12 Notice: Undefined variable: n in /in/Ptef2 on line 13 Notice: Undefined variable: n in /in/Ptef2 on line 12 Notice: Undefined variable: n in /in/Ptef2 on line 13 Notice: Undefined variable: n in /in/Ptef2 on line 12 Notice: Undefined variable: n in /in/Ptef2 on line 13 string(10) "xxxxbbbbbn"
Output for hhvm-3.10.0 - 3.12.0
Notice: Undefined variable: m in /in/Ptef2 on line 8 Notice: Undefined variable: n in /in/Ptef2 on line 12 Notice: Undefined variable: n in /in/Ptef2 on line 13 Notice: Undefined variable: n in /in/Ptef2 on line 12 Notice: Undefined variable: n in /in/Ptef2 on line 13 Notice: Undefined variable: n in /in/Ptef2 on line 12 Notice: Undefined variable: n in /in/Ptef2 on line 13 Notice: Undefined variable: n in /in/Ptef2 on line 12 Notice: Undefined variable: n in /in/Ptef2 on line 13 Notice: Undefined variable: n in /in/Ptef2 on line 12 Notice: Undefined variable: n in /in/Ptef2 on line 13 Notice: Undefined variable: n in /in/Ptef2 on line 12 Notice: Undefined variable: n in /in/Ptef2 on line 13 Notice: Undefined variable: n in /in/Ptef2 on line 12 Notice: Undefined variable: n in /in/Ptef2 on line 13 Notice: Undefined variable: n in /in/Ptef2 on line 12 string(10) "xxxxbbbbbn"
Output for 4.3.0 - 5.0.3
Notice: Undefined variable: m in /in/Ptef2 on line 8 Notice: Undefined variable: n in /in/Ptef2 on line 12 Notice: Undefined variable: n in /in/Ptef2 on line 13 Notice: Undefined variable: n in /in/Ptef2 on line 12 Notice: Undefined variable: n in /in/Ptef2 on line 13 Notice: Undefined variable: n in /in/Ptef2 on line 12 Notice: Undefined variable: n in /in/Ptef2 on line 13 Notice: Undefined variable: n in /in/Ptef2 on line 12 Notice: Undefined variable: n in /in/Ptef2 on line 13 Notice: Undefined variable: n in /in/Ptef2 on line 12 Notice: Undefined variable: n in /in/Ptef2 on line 13 Notice: Undefined variable: n in /in/Ptef2 on line 12 Notice: Undefined variable: n in /in/Ptef2 on line 13 Notice: Undefined variable: n in /in/Ptef2 on line 12 Notice: Undefined variable: n in /in/Ptef2 on line 13 Notice: Undefined variable: n in /in/Ptef2 on line 12 Notice: Undefined variable: n in /in/Ptef2 on line 13 Notice: Undefined variable: n in /in/Ptef2 on line 12 Notice: Undefined variable: n in /in/Ptef2 on line 13 Notice: Undefined variable: n in /in/Ptef2 on line 12 Notice: Undefined variable: n in /in/Ptef2 on line 13 string(10) "xxxxbbbbbn"