2, "\xD0" => 2, "\xE0" => 3, "\xF0" => 4); $i = 0; $len = strlen($s); while ($i < $len) { $ulen = $s[$i] < "\x80" ? 1 : $ulen_mask[$s[$i] & "\xF0"]; $uchr = substr($s, $i, $ulen); $i += $ulen; if (isset($map[$uchr])) { $uchr = $map[$uchr]; $nlen = strlen($uchr); if ($nlen == $ulen) { $nlen = $i; do $s[--$nlen] = $uchr[--$ulen]; while ($ulen); } else { $s = substr_replace($s, $uchr, $i - $ulen, $ulen); $len += $nlen - $ulen; $i += $nlen - $ulen; } } } if (INF === $encoding) return $s; else return function_exists('iconv') ? iconv('UTF-8', $encoding, $s) : $s; } } if (!function_exists('mb_strcut')) { function mb_strcut($str, $start, $length = null, $encoding = '') { $match = array(); // use the regex unicode support to separate the UTF-8 characters into an array preg_match_all( '/./us', $str, $match ); $chars = is_null( $length )? array_slice( $match[0], $start ) : array_slice( $match[0], $start, $length ); return implode( '', $chars ); } } if (!function_exists('mb_detect_encoding')) { function mb_detect_encoding($str, $encoding_list = INF, $strict = false) { if (INF === $encoding_list) $encoding_list = 'UTF-8'; else { if (! is_array($encoding_list)) $encoding_list = array_map('trim', explode(',', $encoding_list)); $encoding_list = array_map('strtoupper', $encoding_list); } foreach ($encoding_list as $enc) { switch ($enc) { case 'ASCII': if (! preg_match('/[\x80-\xFF]/', $str)) return $enc; break; case 'UTF8': case 'UTF-8': if (preg_match('//u', $str)) return $enc; break; default: return strncmp($enc, 'ISO-8859-', 9) ? false : $enc; } } return false; } } if (!function_exists('mb_check_encoding')) { function mb_check_encoding($var = INF, $encoding = INF) { if (INF === $encoding) { if (INF === $var) return false; $encoding = 'UTF-8'; } return false !== mb_detect_encoding($var, array($encoding), true); } }