百科狗-知识改变命运!
--

mb_substr() - mb函数(多字节字符串转化库)

是丫丫呀12个月前 (11-21)阅读数 8#技术干货
文章标签字符

mb_substr()

(PHP 4 >= 4.0.6, PHP 5, PHP 7)

获取部分字符串

说明

mb_substr(string $str,int $start[,int $length= NULL[,string $encoding= mb_internal_encoding()]]): string

根据字符数执行一个多字节安全的substr()操作。位置是从$str的开始位置进行计数。第一个字符的位置是 0。第二个字符的位置是 1,以此类推。

参数

$str

从该string中提取子字符串。

$start

如果$start不是负数,返回的字符串会从$str第$start的位置开始,从 0 开始计数。举个例子,字符串'abcdef',位置0的字符是'a',位置2的字符是'c',以此类推。

如果$start是负数,返回的字符串是从$str末尾处第$start个字符开始的。

$length

$str中要使用的最大字符数。如果省略了此参数或者传入了NULL,则会提取到字符串的尾部。

$encoding

$encoding参数为字符编码。如果省略,则使用内部字符编码。

返回值

mb_substr() - mb函数(多字节字符串转化库)

mb_substr()函数根据$start和$length参数返回$str中指定的部分。

更新日志

版本说明
5.4.8$length传入NULL,则从 start 提取到字符串的结尾处。在之前的版本里,NULL会被当作0来处理。

参见

  • mb_strcut()获取字符的一部分
  • mb_internal_encoding()设置/获取内部字符编码
Passing null as length will not make mb_substr use it's default, instead it will interpret it as 0.

Instead use: 
Thanks Darien from /freenode #php for the following example (a little bit changed).
It just prints the 6th character of $string.
You can replace the digits by the same in japanese, chinese or whatever language to make a test, it works perfect.

(I couldn't replace 0123456789 by chinese numbers for example here, because it's automatically converted into latin digits on this website, look : 
零一二三四
五六七八九)
gilv
As you often need to iterate over UTF-8 characters inside a string, you might be tempted to use mb_substr($text,$i,1).
The problem with this is that there is no "magic" way to find $i-th character inside UTF-8 string, other than reading it byte by byte from the begining. Thus a loop which calls mb_substr($text,$i,1) N times for all possible N values of $i, will take much longer than expected. The larger the $i gets, the longer is the search for $i-th letter. As characters are between 1 to 6 bytes long, one can convince oneself, that the execution time of such loop is actually Theta(N^2), which can be really slow even for moderately long texts.
One way to work around it is to first split your text into an array of letters using some smart preprocessing, and only then iterate over the array.
Here is the idea:

As you can see, the Strings::letters($text) split the text recursively into two parts. Each level of the recursion requires time linear in the length of the string, and there is logarithmic number of levels, so the total runtime is O(N log N), which is still more than theoretically optimal O(N), but sadly this is the best idea I've got.
Note: If borders are out of string - mb_string() returns empty _string_, when function substr() returns _boolean_ false in this case.
Keep this in mind when using "===" comparisions.
Example code:

It's especially confusing when using mbstring with function overloading turned on.
I'm trying to capitalize only the first character of the string and tried some of the examples above but they didn't work. It seems mb_substr() cannot calculate the length of the string in multi-byte encoding (UTF-8) and it should be set explicitly. Here is the corrected version:

cheers!
you can make mb_substr working faster with long strings with usage of ucs-2 encoding.

output:
mb_substr, utf8: 0.26480984687805 . check: string(5) "ст " .
mb_substr, ucs2: 0.0048871040344238 . check: string(5) "ст " .
quick and dirty loop through multibyte string

Array
(
  [numeric] => numeric
  [alpha] => alpha
  [alphalower] => alphalower
  [alphaupper] => alphaupper
  [currency] => currency
  [ascii] => ascii
  [binary] => binary
)
A serious pitfall when using mb_substr() set to HTML-ENTITIES encoding is that the function performs a number of conversions before returning the value, the worst one being that html special characters are not just counted but decoded. 
First letter in upper case 

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)