PHP substr(),mb_substr()及mb_strcut的区别和用法
来源:互联网 发布:asp.net高级编程 编辑:程序博客网 时间:2024/05/04 06:06
举个例子:
<?phpecho mb_substr('这样一来我的字符串就不会有乱码^_^', 0, 7, 'utf-8');?>输出:这样一来我的字
<?phpecho mb_strcut('这样一来我的字符串就不会有乱码^_^', 0, 7, 'utf-8');?>输出:这样一
从上面的例子可以看出,mb_substr是按字来切分字符,而mb_strcut是按字节来切分字符,但是都不会产生半个字符的现象……
mbstring 函数的说明:
php的mbstring扩展模块提供了多字节字符的处理能力,平常最常用的就是用mbstring来切分多字节的中文字符,这样可以避免出现半个字符的情况,由于是php的扩展,它的性能也要比一些自定义的多字节切分函数要好上一些。
mbstring extension提供了几个功能类似的函数,mb_substr和mb_strcut,看看手册上对它们的解释。
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来做切分:
<?php$str = '我是一串比较长的中文-www.webjx.com';echo "mb_substr:" . mb_substr($str, 0, 6, 'utf-8');echo "<br>";echo "mb_strcut:" . mb_strcut($str, 06, 'utf-8');?>输出结果如下:
mb_substr:我是一串比较
mb_strcut:我是
原文地址 http://blog.csdn.net/lijunling2008live/article/details/7734743
- PHP substr(),mb_substr()及mb_strcut的区别和用法
- PHP substr(),mb_substr()及mb_strcut的区别和用法
- PHP substr(),mb_substr()及mb_strcut的区别和用法
- PHP substr(),mb_substr()及mb_strcut的区别和用法
- PHP substr(),mb_substr()及mb_strcut的区别和用法
- PHP substr(),mb_substr()及mb_strcut的区别和用法
- PHP substr(),mb_substr()及mb_strcut的区别和用法
- PHP substr(),mb_substr()及mb_strcut的区别和用法
- PHP substr(),mb_substr()及mb_strcut的区别和用法
- PHP substr(),mb_substr(),mb_strcut的区别和用法
- substr(),mb_substr()及mb_strcut的区别和用法
- substr(),mb_substr()及mb_strcut的区别和用法
- substr(),mb_substr()及mb_strcut的区别和用法
- substr(),mb_substr()及mb_strcut的区别
- mb_substr和mb_strcut的区别
- PHP中substr() mb_substr() mb_struct()的区别和用法
- PHP中substr() mb_substr() mb_struct()的区别和用法
- substr(),mb_substr(),mb_strcut()的使用心得
- 为什么会造车WDT
- 统计学生成绩部分sql语句
- 获取bmp图像的函数.cpp
- Can't create handler inside thread that has not called Looper.prepare()解决办法
- 常用的sql函数
- PHP substr(),mb_substr()及mb_strcut的区别和用法
- Mysql 官方Memcached 插件初步试用感受
- 纹理贴图
- 网络钓鱼现状
- [转]“野鸡大学”名单 中国有三家“野鸡大学”
- 特殊光处理
- 正琢磨着哪个部位咬下去
- 抽象类和接口的区别,使用场景
- thinkphp文章管理代码