没有不值得去解决的问题,也没有不值得去学习的技术!

Implement String Truncation in UTF-8 Encoding Where a Chinese Character Counts as 1 and an English Letter or Digit as 0.5

Compare the search effect for mixed Chinese, English, and numeric keywords with Baidu. Search for: 中困要要要要乱说乱动器8d88847yudghdjuhfhryhw27在要困要要要ssssssssss在要无误仙客shjdjssdddd来要是要要粉黛将军肚要呆要要因. The results are as follows

1. Enter the following into the Baidu search box: 中困要要要要乱说乱动器在要困要要要在要无误仙客来要是要要粉黛将军肚要呆要要因要两手空空在 要手榴弹要要在顺在手榴弹要在右要要要要. The final search results are shown in the image below:

Notice: "要两手" and the words after it are ignored because Baidu's query is limited to 38 Chinese characters.
Notice: “要两手” and the words after it 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 One Chinese character represents 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 code screenshot is as follows (one Chinese character represents 2 lengths, so 38 needs to be multiplied by 2, and “要两手” has a length of 3 Chinese characters, so its length in the function is 6):

The code screenshot is as follows (one Chinese character represents 2 lengths, so 38 needs to be multiplied by 2, and "要两手" has a length of 3 Chinese characters, so its length in the function is 6)
The code screenshot is as follows (one Chinese character represents 2 lengths, so 38 needs to be multiplied by 2, and “要两手” has a length of 3 Chinese characters, so its length in the function is 6)

4. Call the string truncation function, its code and screenshot are as follows:

function qstrlen: if the length is less than 38 Chinese characters, it returns the string length; otherwise, it returns an array.
function qstrlen: if the length is less than 38 Chinese characters, it returns the string length; otherwise, it returns an array.

5. Evaluate and display the prompt message in the web template:

Evaluate and display the prompt message in the web template
Evaluate and display the prompt message in the web template

6. If the final search keyword exceeds 38 Chinese characters in length, the display effect is as follows:

If the final search keyword exceeds 38 Chinese characters in length, the display effect is as follows
If the final search keyword exceeds 38 Chinese characters in length, the display effect is as follows

7. Compare the search effect for mixed Chinese, English, and numeric keywords with Baidu. Search for: 中困要要要要乱说乱动器8d88847yudghdjuhfhryhw27在要困要要要ssssssssss在要无误仙客shjdjssdddd来要是要要粉黛将军肚要呆要要因. The results are as follows:

Baidu's prompt message for 3 Chinese characters; its length is not strictly the length of 3 Chinese characters.
Baidu’s prompt message for 3 Chinese characters; its length is not strictly the length of 3 Chinese characters.
Compare the search effect for mixed Chinese, English, and numeric keywords with Baidu. Search for: 中困要要要要乱说乱动器8d88847yudghdjuhfhryhw27在要困要要要ssssssssss在要无误仙客shjdjssdddd来要是要要粉黛将军肚要呆要要因. The results are as follows
Compare the search effect for mixed Chinese, English, and numeric keywords with Baidu. Search for: 中困要要要要乱说乱动器8d88847yudghdjuhfhryhw27在要困要要要ssssssssss在要无误仙客shjdjssdddd来要是要要粉黛将军肚要呆要要因. The results are as follows

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系