Under UTF-8 encoding, the realization of string interception, a Chinese character length is 1, and an English letter and number length is 0.5;
1. Enter in the Baidu search box: in the middle of the poor If you want a grenade, you have to be in the grenade, you need it on the right, and the final search results are as follows:

Tip: “To both hands” and the following words are ignored, because Baidu’s query is limited to 38 Chinese characters.
2. Write the function qstrlen, the code is as follows:
/**
* dstrlen // kcuc
* @param string $str
* @return string $count a Chinese character means two lengths
*/
function qstrlen($str) {
$count = 0;
$len = 0;
for ($i = 0; $i < strlen($str); $i++) {
$value = ord($str[$i]);
if($value > 127) {
$count++;
if ($value >= 192 && $value <= 223) {
$i++;
} elseif ($value >= 224 && $value <= 239) {
$i = $i + 2;
} elseif ($value >= 240 && $value <= 247) {
$i = $i + 3;
}
}
$count++;
if ($count > 76 && $len == 0) {
$len = $i;
$subject = mb_strcut($str, 0, $len,UTF-8);
}
if ($count > 82) {
return[$subject, str_replace($subject, ”, mb_strcut($str, 0, $i, ‘utf-8’))];
break;
}
}
if ($count > 76 && $count <= 82) {
return[$subject, str_replace($subject, ”, mb_strcut($str, 0, null, ‘utf-8’))];
}
return $count;
}
3. The screenshot of the code is as follows (one Chinese character represents 2 lengths, so 38 needs to be multiplied by 2, and “two hands” is the length of 3 Chinese characters, so the length in the function is 6):

The screenshot of the code is as follows (one Chinese character represents 2 lengths, so 38 needs to be multiplied by 2, and “to both hands” is the length of 3 Chinese characters, so the length is 6 in the function)
4. Call the intercepted string function, its code and screenshot are as follows:

function Qstrlen, if the length is less than 38 Chinese characters, it will return a long string, otherwise return an array;
5. Determine and display the prompt information in the web page template:
6. If the length of the final search keyword exceeds 38 Chinese characters, the display effect is as follows:

If the length of the final search keyword exceeds 38 Chinese characters, the display effect is as follows
7. Comparing the search effect of Chinese and English plus numbers with Baidu hw27 is going to have to be sleepy, and it needs to be ssssssssss.

The prompt information of Baidu’s 3 Chinese characters, its length is not the strict length of 3 Chinese characters;

