Home > Backend Development > PHP Tutorial > PHP中文处理 中文字符串截取(mb_substr)和获取中文字符串字数_PHP

PHP中文处理 中文字符串截取(mb_substr)和获取中文字符串字数_PHP

WBOY
Release: 2016-06-01 12:13:49
Original
787 people have browsed it
一、中文截取:mb_substr()

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

$str,需要截断的字符串
$start,截断开始处,起始处为0
$length,要截取的字数
$encoding,网页编码,如utf-8,GB2312,GBK

实例:
复制代码 代码如下:
$str=':http://www.bitsCN.com';
echo mb_substr($str,0,4,'utf-8');//截取头5个字,假定此代码所在php文件的编码为utf-8
?>
结果显示:


二、获取中文长度:mb_strlen()

mb_strlen( $str, $encoding )

$str,要计算长度的字符串
$encoding,网页编码,如utf-8,GB2312,GBK

实例:
复制代码 代码如下:
$str=':http://www.bitsCN.com';
echo mb_strlen($str,'utf-8');//假定此代码所在php文件的编码为utf-8
?>
结果显示:24
Related labels:
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