Home  >  Article  >  Backend Development  >  PHP Chinese processing Chinese string interception (mb_substr) and obtaining the number of Chinese string characters

PHP Chinese processing Chinese string interception (mb_substr) and obtaining the number of Chinese string characters

高洛峰
高洛峰Original
2017-01-03 15:12:071128browse

1. Chinese interception: mb_substr()

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

$str, the string that needs to be truncated
$start , the starting point of truncation, the starting point is 0
$length, the number of words to be intercepted
$encoding, web page encoding, such as utf-8, GB2312, GBK

Example:

<?php 
$str=&#39;PHP中文网://m.sbmmt.com&#39;; 
echo mb_substr($str,0,4,&#39;utf-8&#39;);//截取头5个字,假定此代码所在php文件的编码为utf-8 
?> 
结果显示:PHP中文网

2. Get the Chinese length: mb_strlen()

mb_strlen( $str, $encoding )

$str, the string to calculate the length
$encoding, web page encoding, such as utf-8, GB2312, GBK

Example:

<?php 
$str=&#39;PHP中文网://m.sbmmt.com&#39;; 
echo mb_strlen($str,&#39;utf-8&#39;);//假定此代码所在php文件的编码为utf-8 
?> 
结果显示:24

For more PHP Chinese processing Chinese string interception (mb_substr) and obtaining Chinese string word count related articles, please pay attention to the PHP Chinese website!

Statement:
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