3v4l.org

run code in 300+ PHP versions simultaneously
<?php function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true) { if($suffix) $suffixStr = "…"; else $suffixStr = ""; if(function_exists("mb_substr")) return mb_substr($str, $start, $length, $charset).$suffixStr; elseif(function_exists('iconv_substr')) { return iconv_substr($str,$start,$length,$charset).$suffixStr; } $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/"; $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/"; $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/"; $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/"; preg_match_all($re[$charset], $str, $match); $slice = join("",array_slice($match[0], $start, $length)); return $slice.$suffixStr; } function csubstr($str, $start=0, $length, $charset="utf-8", $suffix=true) { if(function_exists("mb_substr")) return mb_substr($str, $start, $length, $charset); $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/"; $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/"; $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/"; $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/"; preg_match_all($re[$charset], $str, $match); $slice = join("",array_slice($match[0], $start, $length)); if($suffix) return $slice."…"; return $slice; } $a = '测试这是一个测试'; //$a = 'xxxxbbbbbnnn'; $b = csubstr($a,0,5,10); var_dump($b); ?>
Output for 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
Deprecated: Optional parameter $start declared before required parameter $length is implicitly treated as a required parameter in /in/RBdKo on line 3 Deprecated: Optional parameter $start declared before required parameter $length is implicitly treated as a required parameter in /in/RBdKo on line 24 Fatal error: Uncaught ValueError: mb_substr(): Argument #4 ($encoding) must be a valid encoding, "10" given in /in/RBdKo:27 Stack trace: #0 /in/RBdKo(27): mb_substr('\xE6\xB5\x8B\xE8\xAF\x95\xE8\xBF\x99\xE6\x98\xAF\xE4\xB8\x80...', 0, 5, '10') #1 /in/RBdKo(41): csubstr('\xE6\xB5\x8B\xE8\xAF\x95\xE8\xBF\x99\xE6\x98\xAF\xE4\xB8\x80...', 0, 5, 10) #2 {main} thrown in /in/RBdKo on line 27
Process exited with code 255.
Output for 8.0.0 - 8.0.30
Deprecated: Required parameter $length follows optional parameter $start in /in/RBdKo on line 3 Deprecated: Required parameter $length follows optional parameter $start in /in/RBdKo on line 24 Fatal error: Uncaught ValueError: mb_substr(): Argument #4 ($encoding) must be a valid encoding, "10" given in /in/RBdKo:27 Stack trace: #0 /in/RBdKo(27): mb_substr('\xE6\xB5\x8B\xE8\xAF\x95\xE8\xBF\x99\xE6\x98\xAF\xE4\xB8\x80...', 0, 5, '10') #1 /in/RBdKo(41): csubstr('\xE6\xB5\x8B\xE8\xAF\x95\xE8\xBF\x99\xE6\x98\xAF\xE4\xB8\x80...', 0, 5, 10) #2 {main} thrown in /in/RBdKo on line 27
Process exited with code 255.
Output for 4.3.2 - 4.3.11, 4.4.0 - 4.4.9, 5.0.0 - 5.0.5, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33
Warning: mb_substr(): Unknown encoding "10" in /in/RBdKo on line 27 bool(false)
Output for 4.3.0 - 4.3.1
Warning: mb_substr() [http://www.php.net/function.mb-substr]: Unknown encoding "10" in /in/RBdKo on line 27 bool(false)

preferences:
297.91 ms | 401 KiB | 460 Q