php获取字符串宽度。
/** * 获取字符串宽度,半角字符1=1,非半角角字符1=2 * @param string $str * @return float|int */ function strwidth($str) { $charcount = mb_strlen($str, 'utf-8'); $pattern = '/[[:print:][:space:]]/u'; $halfcount = 0; if (preg_match_all($pattern, $str, $_match)) { $halfcount = sizeof($_match[0]); } return $halfcount + ($charcount - $halfcount) * 2; }