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;

中英文加数字混合关键词搜索效果与百度对比,搜索:中困要要要要乱说乱动器8d88847yudghdjuhfhryhw27在要困要要要ssssssssss在要无误仙客shjdjssdddd来要是要要粉黛将军肚要呆要要因,结果如下

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:

提示:"要两手" 及其后面的字词均被忽略,因为百度的查询限制在38个汉字以内。
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):

代码截图如下(一个汉字表示2个长度,因此38需要乘以2,且“要两手”为3个汉字的长度,因此在函数中长度为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,如果长度小于38个汉字,则返回字符串长,否则返回数组;
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:

在网页模板中判断并显示提示信息
Judgment and display 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:

最终搜索关键词如果长度超出38个汉字,显示效果如下
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.

百度的3个汉字的提示信息,其长度并非严格的3个汉字的长度;
The prompt information of Baidu’s 3 Chinese characters, its length is not the strict length of 3 Chinese characters;
中英文加数字混合关键词搜索效果与百度对比,搜索:中困要要要要乱说乱动器8d88847yudghdjuhfhryhw27在要困要要要ssssssssss在要无误仙客shjdjssdddd来要是要要粉黛将军肚要呆要要因,结果如下
Comparing the search effect of Chinese and English plus numbers and Baidu W27 is in trouble

 

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.