In the development of the project, it is often encountered to calculate the length of a string (combination of Chinese and English). Due to different product requirements, the length requirements of each Chinese are also different.
Solve the string length under utf-8 encoding (you can customize how many bytes each Chinese and English count)
/** * Calculate the string length * @author Timothy * @param null $string The string to be calculated, default null * @param int $chinese How many bytes Chinese occupies, default 2 * @param int $other Other characters count as several bytes, default 1 * @return int length returned */ private function utf8_strlen($string = null, $chinese = 2, $other = 1) { // break the string into units preg_match_all("/./us", $string, $match); $pattern = '/[^\x00-\x80]/'; $count = 0; foreach ($match[0] as $key => $value) { $count = preg_match($pattern, $value) ? ($count + $chinese) : ($count + $other); } return $count; } Note: 1. preg_match_all("/./us", $string, $match);//"/./us", u means the default utf-8 encoding, s (PCRE_DOTALL) means If this modifier is set, the dot metacharacter (.) in the pattern matches all characters, including newlines. Without this setting, newlines are not included. 2. $pattern = '/[^\x00-\x80]/';//means matching Chinese