Home > Backend Development > PHP Tutorial > Detailed introduction to PHP truncation function mb_substr()_PHP tutorial

Detailed introduction to PHP truncation function mb_substr()_PHP tutorial

WBOY
Release: 2016-07-20 11:03:40
Original
986 people have browsed it

In php, the mb_substr() function is a function used to intercept Chinese and English. It can quickly and easily solve the problem of intercepting the specified character length. Let me introduce it to you below. ​

Tip: mb_substr is not supported by default in php. We need to find php.ini in the windows directory, open it for editing, search for mbstring.dll, find ;extension=php_mbstring.dll and remove the previous; sign before it can be used. mb_substr function.

Function: mb_substr( $str, $start, $length, $encoding )

$str, the string that needs to be truncated
$start, truncation start point
$length, length (note, this is different from mb_strimwidth, 1 represents a Chinese character)
$encoding, encoding, I set it to utf-8

Example: Truncate the article title to 15 characters

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

 
$str = '这样一来我的字符串就不会有乱码^_^';

echo "mb_substr:" . mb_substr($str, 0, 7, 'utf-8');
//结果:这样一来我的字
echo "
";

echo "mb_strcut:" . mb_strcut($str, 0, 6, 'utf-8');
//结果:这样
?>

$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 ?>
In summary, it appears that he can solve the problem of intercepting Chinese characters http://www.bkjia.com/PHPjc/445280.htmlwww.bkjia.com
true
http: //www.bkjia.com/PHPjc/445280.htmlTechArticleThe mb_substr() function in php is a function used to intercept Chinese and English, which can easily and quickly solve the interception specification Regarding the issue of character length, let me introduce it to you below. Tip: mb_subs...
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