浅析PHP substr,mb_substr以及mb_strcut的区别和用法

PHP substr()函数可以 分割文字,但要分割的文字如果包括中文字符往往会遇到问题,这时可以用mb_substr()/mb_strcut这个函数,mb_substr() /mb_strcut的用法与substr()相似,只是在mb_substr()/mb_strcut最后要加入多一个参数,以设定字符串的编码,但是 一般的服务器都没打开php_mbstring.dll,需要在php.ini在把php_mbstring.dll打开。

举个例子:

<div class="codetitle"><a style="CURSOR: pointer" data="29066" class="copybut" id="copybut29066" onclick="doCopy('code29066')"> 代码如下:
<div class="codebody" id="code29066">
<?php
echo mbsubstr('这样一来我的字符串就不会有乱码^^',7,'utf-8');
?>

<FONT style="COLOR: #ff0000">输出:这样一来我的字
<div class="codetitle"><a style="CURSOR: pointer" data="20780" class="copybut" id="copybut20780" onclick="doCopy('code20780')"> 代码如下:
<div class="codebody" id="code20780">
<?php
echo mbstrcut('这样一来我的字符串就不会有乱码^^','utf-8');
?>

<FONT style="COLOR: #ff0000">输出:这样一
从上面的例子可以看出,mb_substr是按字来切分字符,而mb_strcut是按字节来切分字符,但是都不会产生半个字符的现象……

mbstring 函数的说明:

php的mbstring扩展模块提供了多字节字符的处理能力,平常最常用的就是用mbstring来切分多字节的中文字符,这样可以避免出现半个字符的情况,由于是php的扩展,它的性能也要比一些自定义的多字节切分函数要好上一些。
mbstring extension提供了几个功能类似的函数,mb_substr和mb_strcut,看看手册上对它们的解释。
<div class="codetitle"><a style="CURSOR: pointer" data="98753" class="copybut" id="copybut98753" onclick="doCopy('code98753')"> 代码如下:
<div class="codebody" id="code98753">
mb_substr
mb_substr() returns the portion of str specified by the start and length parameters.
mb_substr() performs multi-byte safe substr() operation based on number of characters. Position is counted from the beginning of str. First character's position is 0. Second character position is 1,and so on.
mb_strcut
mb_strcut() returns the portion of str specified by the start and length parameters.
mb_strcut() performs equivalent operation as mb_substr() with different method. If start position is multi-byte character's second byte or larger,it starts from first byte of multi-byte character.
It subtracts string from str that is shorter than length AND character that is not part of multi-byte string or not being middle of shift sequence.

再举个例子,有一段文字,分别用mb_substr和mb_strcut来做切分:

PLAIN TEXT
CODE:

<div class="codetitle"><a style="CURSOR: pointer" data="38274" class="copybut" id="copybut38274" onclick="doCopy('code38274')"> 代码如下:
<div class="codebody" id="code38274">
<?php
$str = '我是一串比较长的中文-www.webjx.com';
echo "mb_substr:" . mb_substr($str,6,'utf-8');
echo "
";
echo "mb_strcut:" . mb_strcut($str,'utf-8');
?>

输出结果如下:

<FONT style="COLOR: #ff0000">mb_substr:我是一串比较
mb_strcut:我是

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

mb_strcutmb_substrsubstr

相关推荐


如何选择合适的 C++ Web 开发框架?
利用 C++ 框架构建高并发 Web 应用的策略
用 C++ 框架开发跨平台图形应用程序
golang框架中安全编码实践的最佳指南是什么?
golang框架与其他语言框架在设计理念上的区别有哪些?
C++ 图形框架与其他语言框架的比较
C++ 框架与其他 Web 开发框架的对比分析
使用 C++ 框架构建大型项目最佳实践
C++ 框架如何提高大型项目开发效率
C++ 框架中依赖注入的持续集成与部署工具
如何与社区协作和贡献到自定义 Golang 框架?
C++ 框架在大型项目中如何实现模块化开发
使用 C++ 框架开发跨平台 Web 应用
C++ 框架在大型项目中的优缺点
golang框架在性能上的优势体现在哪些方面?
C++ 框架在嵌入式系统内存优化中的优势
golang框架在人工智能与机器学习中的作用
如何扩展 Golang 框架以支持特定功能?
如何利用 Go Modules 和依赖项管理来自定义 Golang 框架?
Golang 框架中的性能优化技巧