Home  >  Article  >  php教程  >  PHP中文处理 中文字符串截取(mb_substr)和获取中文字符串字数

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

WBOY
WBOYOriginal
2016-06-13 12:04:131195browse

一、中文截取:mb_substr()

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

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

实例:

复制代码 代码如下:


$str='脚本之家:http://www.jb51.net';
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.jb51.net';
echo mb_strlen($str,'utf-8');//假定此代码所在php文件的编码为utf-8
?>
结果显示:24

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