简体   繁体   English

我为什么要使用strtolower()而不是mb_strtolower()?

[英]Why should I use strtolower() rather than mb_strtolower()?

I understand the usage/behaviour difference between strtolower() and mb_strtolower() functions. 我理解strtolower()mb_strtolower()函数之间的用法/行为差异。 And it has been asked before :) 之前有人问过:)

But I don't understand - what is the current purpose of strtolower ? 但我不明白 - strtolower目前的目的是什么?

Is it available because of backwards compatibility? 是否因为向后兼容性而可用? Or is there some use cases when strtolower is preferable? 或者,当strtolowerstrtolower时,是否存在一些使用案例? It seems that mb_strtolower() is safer and more versatile so I am tempted to use it everywhere... 似乎mb_strtolower()更安全,更通用,所以我很想在任何地方使用它...

Not really backward compatibility, but the ability to use code wihtout additionally loaded multibyte string extension. 不是真正的向后兼容性,而是能够使用代码而不另外加载多字节字符串扩展。 So a question of portability of the code. 所以代码的可移植性问题。

strtolower() is always available. strtolower()始终可用。 mb_strtolower() is only available if the additional mbstring module is loaded. mb_strtolower()仅在加载其他mbstring模块时可用。 The function overloading feature allows to use the "generic" call to strtolower() which will actually execute mb_strtolower() if configured such. 函数重载功能允许使用对strtolower()的“通用”调用,如果配置了这样,它将实际执行mb_strtolower() So there actually is no need to explicitly code mb_strtolower() at all... 所以实际上根本没有必要明确地编码mb_strtolower() ...

This makes code more portable, since it can run on systems with or without the mbstring extension. 这使得代码更具可移植性,因为它可以在具有或不具有mbstring扩展名的系统上运行。

The strtolower() is a native function and will always be there compared to mb_strtolower() which is only available if the mbstring module is installed \\ enabled. strtolower()是一个本机函数,与mb_strtolower()相比总是存在,只有在安装了mbstring模块时才可用。

But on the question why one should be used over the other, well that is quite simple. 但是关于为什么一个人应该被用在另一个上的问题,这很简单。 You can use them both and only use mb_strtolower() when its needed and or is available. 您可以使用这两个只有使用mb_strtolower()的需要和或可用。


If you script something you should not use any special characters in scripts, filenames, db structures, etc as it should be language independent to improve portability and by doing so it will remove inconsistency's between operating systems. 如果您编写脚本,则不应在脚本,文件名,数据库结构等中使用任何特殊字符,因为它应该与语言无关以提高可移植性,这样做可以消除操作系统之间的不一致。 Scripts can behave differently in other environments, for example Linux is case-sensitive on fs / db operations where Windows is not. 脚本在其他环境中的行为可能有所不同,例如Linux在Windows不具备的fs / db操作上区分大小写。

A script should not have to deal with its own special characters as it is unnecessary and it will: 脚本应该有对付自己的特殊字符,因为它是不必要的,它会:

  • Cost performance, strtolower() is much faster than mb_strtolower() 性价比, strtolower()mb_strtolower()快得多
  • Lose portability if mbstring is not available on the server. 如果服务器上没有mbstring,则丢失可移植性。

An example is if you use other systems like the PSR-4 autoloader . 例如,如果您使用其他系统,如PSR-4自动装载机

$var = new \ns\ÇavA();

Will attempt to load "Çava.php" instead of "çava.php" because it fails lowering the case due to the fact that its using the native strtolower() functions creating an inconstancy. 将尝试加载“Çava.php”而不是“çava.php”,因为由于它使用本机strtolower()函数创建了一个不稳定的事实,它无法降低案例。 Instead, just keep it ASCII. 相反,只需保持ASCII。


mb_strtolower() should only be used for user input/output where strtolower() should be used for system orientated case formatting. mb_strtolower()只应用于用户输入/输出,其中strtolower()应用于面向系统的大小写格式。

This being said; 这是说; strtolower() will be sufficient for most of anyone's needs, even for user input/output but then the question arises where and how you apply that data. strtolower()足以满足大多数人的需求,即使对于用户输入/输出也是如此,但问题出现在何处以及如何应用该数据。 Keep in mind if its just for output to the user there is always the CSS text-transform method. 请记住,如果只是输出给用户,总会有CSS 文本转换方法。

Short answer 简短的回答

  • You are not always sure the mbstring extension will be available in your environment 您并不总是确定您的环境中可以使用mbstring扩展
  • strtolower() is significantly faster. strtolower()明显更快。

A bit more 多一点

The mbstring extension is a non-default PHP extension. mbstring扩展是非默认的PHP扩展。 This means it is not enabled by default, though in my experience I have never encountered a hosting provider that did not have it enabled. 这意味着它默认不启用,但根据我的经验,我从未遇到过没有启用它的托管服务提供商。

Your question made me wonder if they would have a difference in performance, so I wrote a quick benchmark function you can find here . 你的问题让我想知道他们是否会在性能方面有所不同,所以我写了一个你可以在这里找到的快速基准测试功能。

From the few tests I ran on some random strings, it seems strtolower() is at least 50 times faster in it's execution. 从我在一些随机字符串上运行的几个测试中,似乎strtolower() 执行时至少快50倍

If you know you are dealing with strings that are compatible with strtolower() and you have a really big amount of them, it may be worth examining strtolower() instead. 如果你知道你正在处理与strtolower()兼容的字符串并且你有很多它们,那么可能值得检查strtolower()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM