PHP intercepts the mb_substr() and mb_strcut() functions of Chinese string length

黄舟
Release: 2023-03-05 12:44:02
Original
1324 people have browsed it

The substr() function can be used to split text, but if the text to be split includes Chinese characters, you will often encounter problems. In this case, you can use the mb_substr()/mb_strcut function. The usage of mb_substr()/mb_strcut is the same as that of substr( ) is similar, except that one more parameter needs to be added at the end of mb_substr()/mb_strcut to set the encoding of the string. However, most servers do not open php_mbstring.dll. You need to open php_mbstring.dll in php.ini.

For example:

<?php
echo mb_substr(‘这样一来我的字符串就不会有乱码^_^’, 0, 7, ‘utf-8′);
?>
Copy after login

Output: This way my words

<?php
echo mb_strcut(‘这样一来我的字符串就不会有乱码^_^’, 0, 7,’utf-8′);
?>
Copy after login

Output: This way

From above As can be seen from the example, mb_substr splits characters by words, while mb_strcut splits characters by bytes, but neither will produce half a character...

The above paragraph is excerpted from the Internet , the results are all obtained through my own testing.

Personal understanding:
mb_substr() function represents a unit for English or Chinese characters.
mb_strcut() function is 3 units for Chinese characters and 1 unit for English characters.

For example:

<?php
$str = “这样abcd一来”;
echo “mb_substr:”.mb_substr($str, 0, 5, ‘utf-8′);
echo “<br>”;
echo “mb_strcut:”.mb_strcut($str, 0, 8, ‘utf-8′);
?>
Copy after login

The output result is as follows:

mb_substr: 这样abc
mb_strcut:  这样ab
Copy after login

Attachment:

The difference between strlen and mb_strlen:

<?php 
$str=”中文a字1符”; 
echo strlen($str); 
echo “<br />”; 
echo mb_strlen($str,’UTF8′); 
?>
Copy after login

Output result:

14 
6
Copy after login

The above is the content of mb_substr() and mb_strcut() functions that PHP uses to intercept the length of Chinese strings. For more related content, please pay attention to PHP Chinese Net (www.php.cn)!


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!