今天遇到需要按字节截取GBK和UTF8字符串的需求。记录一下代码片段。
function substrMB($str, $minLen, $maxLen, $charset = "utf8", $append = false, $default = "") {
$length = strlen($str);
if ($length <= $minLen) {
$str = $default;
} elseif ($length <= $maxLen) {
//DO NOTHING
} else {
$strLast = array();
$i = ……