Home > Backend Development > PHP Tutorial > mb_substr Chinese interception knowledge_PHP tutorial

mb_substr Chinese interception knowledge_PHP tutorial

WBOY
Release: 2016-07-13 17:06:18
Original
889 people have browsed it

The mb_substr function is an upgraded version of the substr function. mb_substr supports Chinese interception and will not cause garbled characters. Let me introduce the basic knowledge of mb_substr.

Example

The code is as follows
 代码如下 复制代码

$str = ‘这样一来我的字符串就不会有乱码^_^’;
echo “mb_substr:” . mb_substr($str, 0, 7, ‘utf-8′);
//结果:这样一来我的字
echo “
”;
echo “mb_strcut:” . mb_strcut($str, 0, 6, ‘utf-8′);
//结果:这样
?>

Copy code

$str = ‘This way my string will not be garbled^_^’;

echo “mb_substr:” . mb_substr($str, 0, 7, ‘utf-8′);

//Result: In this way, my words

echo “
”;

echo “mb_strcut:” . mb_strcut($str, 0, 6, ‘utf-8′);

//Result: like this
 代码如下 复制代码

if(mb_strlen($str, "gbk") >20)

{

  $str = mb_substr($str, 0, 20, "gbk");

}

?>


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

In actual use, it needs to be used in conjunction with mb_strlen. The usage of mb_strlen is similar to strlen, except that there is an additional encoding parameter: mb_strlen("I am the character whose length is to be calculated", "gbk"); the result is 11.

Detailed usage examples:
For example, if the string exceeds 20, it needs to be intercepted:

The code is as follows Copy code
if(mb_strlen($str, "gbk") >20)

{

} Note:
Make sure you have the php_mbstring.dll file under Windows/system32. If not, copy it from your Php installation directory extensions into Windows/system32.
2. Find php.ini in the windows directory, open it for editing, search for mbstring.dll, and find ;extension=php_mbstring.dll remove the previous; sign so that the mb_substr function can take effect http://www.bkjia.com/PHPjc/630698.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/630698.htmlTechArticleThe mb_substr function is an upgraded version of the substr function. mb_substr supports Chinese interception and will not cause garbled characters. Let me introduce it below Let’s take a look at the basic knowledge of mb_substr. Example code is as follows Copy the code...
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