PHP截取中英文混合防止乱码
程序员文章站
2024-01-14 08:00:40
...
php代码
function utf8_substr($str, $from, $len = 180, $suffix = '') { $str = trim($str); $str = strip_tags($str); $pre_len = strlen($str); $str = preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'. $from .'}'.'((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'. $len .'}).*#s', '$1', $str); if ($pre_len != strlen($str)) { $str .= $suffix; } return $str; }