是否应该有类似“bytelen”的东西? (与“strlen”一起)?
在我看来,“strlen”函数应该只返回字符串中的字符数。没有别的了。确实如此,无论是计算 ASCII 字符还是 Unicode 字符。字符就是一个字符,指向 ASCII 表或 UTF-8 表上的给定位置。而已。
如果您出于某种原因想知道字符串的字节长度,那么您应该使用不同的函数。我是 PHP 脚本编写的新手,所以我还没有找到该功能。 (应该类似于“bytelen()”?)
In my opinion the 'strlen' function should only return the number of characters in a string. Nothing else. And it does, whether it counts ASCII characters or Unicode characters. A character is a character, pointing to a given position on an ASCII table or a UTF-8 table. Nothing more.
If you would like to know, for whatever reason, the byte-length of a string, then you should use a differtent function. I am a newby in PHP scripting, so I did not find that function yet. (Should be something like 'bytelen()'?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
mb_strlen()
的作用你在追赶。mb_strlen()
does what you're after.是的,这将是最合乎逻辑的设计。然而,PHP 从一开始就没有计划支持多字节字符集。相反,多年来它一直在以一种混乱的方式发展。您已将您的问题标记为 PHP 4,但 PHP 5 还没有像样的 Unicode 支持(而且我认为它在不久的将来不会改变)。
无论如何,有几个原因:
PHP 不是由企业规则控制的集中设计的公司拥有的闭源商业产品。
PHP 于 1995 年作为个人项目发布,由某人在其静态主页中需要一些功能:当时不需要 Unicode 支持。
如果您修改像 strlen() 这样的核心函数,您必须以不会破坏以前功能的方式进行操作。这并不容易。编写新的单独函数要容易得多。
更新
抱歉,我忘记了你问题的第二部分。如果您需要处理 Unicode 字符串,则必须使用一组单独的函数:
您可能还会发现这些章节很有趣:
请注意每个版本所需的 PHP 版本您计划使用的功能; PHP 4 已经很老了。
Yes, that would be most logical design. However, PHP has not been planned to support multibyte charsets from the beginning. Instead, it's been evolving along the years in a sort of chaotic manner. You've tagged your question as PHP 4 but PHP 5 does not have a decent Unicode support yet (and I don't think it'll change in a nearby future).
There're a few reasons for this anyway:
PHP is not a closed-source commercial product owned by a company with a centralized design controlled by enterprise rules.
PHP was released in 1995 as a personal project by someone who needed some functionality in his static home page: at that time, it had no need for Unicode support.
If you modify core functions like strlen() you must do it in a way that it doesn't break previous functionality. It's not easy. Writing a new separate function is much easier.
Update
Sorry, I forgot the second part of your question. If you need to handle Unicode strings you have to use a separate set of functions:
You might also find these chapters interesting:
Please take note of the PHP version required by each function you are planning to use; PHP 4 is pretty old.
如果我没有严重误解你的话,那么 strlen() 你的“bytelen()”,正如其他回复中提到的那样。
strlen()本身不支持utf-8或其他多字节字符集;如果你想要一个合适的 strlen(),你需要 mb_strlen()。
Pentium10 的函数 strBytes($str),从浏览它(未测试)来看,如果您知道您的编码是 utf-8 并且您坚持使用超低版本,那么它似乎是一个不错的选择由于某种原因PHP4。
(我确实建议您查看 Álvaro G. Vicario 的帖子,了解此行为背后的原因。PHP6 将提供适当的原生 UTF-8 支持。)
If I'm not grossly misunderstanding you, then strlen() is your 'bytelen()', as alluded to in the other responses here.
strlen() itself has no support for utf-8 or other multi-byte character sets; if you want a proper strlen(), you'll need mb_strlen().
Pentium10's function strBytes($str), from glancing over it (not testing) looks like it would be a good alternative if you know your encoding is utf-8 and you're stuck with a super low version of PHP4 for some reason.
(And I do recommend taking a look at Álvaro G. Vicario's post for the reasons behind this behaviour. Proper, native UTF-8 support is due to come with PHP6.)