strlen() - 获取字符串长度 - php 字符串函数
strlen()
(PHP 4, PHP 5, PHP 7)
获取字符串长度
说明
strlen (string $string ) : int返回给定的字符串 $string 的长度。
参数
$string
需要计算长度的字符串。
返回值
成功则返回字符串 $string 的长度;如果 $string 为空,则返回 0。
更新日志
版本 | 说明 |
---|---|
5.3.0 | Prior versions treated arrays as the string Array, thus returning
a string length of 5 and emitting an E_NOTICE level error. |
范例
strlen() 范例
注释
Note:
strlen() returns the number of bytes rather than the number of characters in a string.
Note:
strlen() returns NULL
when executed on arrays, and
an E_WARNING
level error is emitted.
参见
count()
计算数组中的单元数目,或对象中的属性个数mb_strlen()
获取字符串的长度
I want to share something seriously important for newbies or beginners of PHP who plays with strings of UTF8 encoded characters or the languages like: Arabic, Persian, Pashto, Dari, Chinese (simplified), Chinese (traditional), Japanese, Vietnamese, Urdu, Macedonian, Lithuanian, and etc. As the manual says: "strlen() returns the number of bytes rather than the number of characters in a string.", so if you want to get the number of characters in a string of UTF8 so use mb_strlen() instead of strlen(). Example:
The easiest way to determine the character count of a UTF8 string is to pass the text through utf8_decode() first: utf8_decode() converts characters that are not in ISO-8859-1 to '?', which, for the purpose of counting, is quite alright.
We just ran into what we thought was a bug but turned out to be a documented difference in behavior between PHP 5.2 & 5.3. Take the following code example: // Begin Output: Length: 0 Length: 0 Length: 0 Null length is Zero Null length is still Zero !is_null(): $foo is probably null isset(): $foo is probably null !is_null(): $bar is truly an empty string isset(): $bar is truly an empty string // End Output So it would seem you need either is_null() or isset() in addition to strlen() if you care whether or not the original value was null.
There's a LOT of misinformation here, which I want to correct! Many people have warned against using strlen(), because it is "super slow". Well, that was probably true in old versions of PHP. But as of PHP7 that's definitely no longer true. It's now SUPER fast! I created a 20,00,000 byte string (~20 megabytes), and iterated ONE HUNDRED MILLION TIMES in a loop. Every loop iteration did a new strlen() on that very, very long string. The result: 100 million strlen() calls on a 20 megabyte string only took a total of 488 milliseconds. And the strlen() calls didn't get slower/faster even if I made the string smaller or bigger. The strlen() was pretty much a constant-time, super-fast operation So either PHP7 stores the length of every string as a field that it can simply always look up without having to count characters. Or it caches the result of strlen() until the string contents actually change. Either way, you should now never, EVER worry about strlen() performance again. As of PHP7, it is super fast! Here is the complete benchmark code if you want to reproduce it on your machine:
if your text contain UTF8 character (arabic character for example), the mb_substr can use instead of strlen. but if the string contain UTF16 (wmoji, smily) then you can use this method to get correct length:
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)