set_locale(LC_CTYPE, 'C'); 的作用是什么?实际上呢?
当我的 PHP 脚本以 UTF-8 编码运行时,使用非 ASCII 字符,一些 PHP 函数(如 strtolower()
)不起作用。
我可以使用 mb_strtolower,但该脚本可以在各种不同的平台和配置上运行,并且多字节字符串扩展可能不可用。我可以在使用前检查该函数是否存在,但我的代码中散布着字符串函数,并且不想替换每个实例。
有人建议使用 set_locale(LC_CTYPE, 'C')
,他说这可以使字符串函数正常工作。这听起来不错,但我不想在不确切了解它在做什么的情况下引入该更改。我之前使用过 set_locale 来更改数字的格式,但是我之前没有使用过 LC_CTYPE 标志,而且我不太明白它的作用。值'C'
是什么意思?
When my PHP script is run with UTF-8 encoding, using non-ASCII characters, some PHP functions like strtolower()
don't work.
I could use mb_strtolower, but this script can be run on all sorts of different platforms and configurations, and the multibyte string extension might not be available. I could check whether the function exists before use, but I have string functions littered throughout my code and would rather not replace every instance.
Someone suggested using set_locale(LC_CTYPE, 'C')
, which he says causes the string functions to work correctly. This sounds fine, but I don't want to introduce that change without understanding exactly what it is doing. I have used set_locale to change the formatting of numbers before, but I have not used the LC_CTYPE
flag before, and I don't really understand what it does. What does the value 'C'
mean?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
C
的意思是“使用任何硬编码的语言环境”(并且由于大多数 *NIX 程序都是用 C 编写的,因此称为C
)。但是,它通常不是 UTF-8 语言环境。如果您使用多字节字符集(例如 UTF-8),则您无法使用常规字符串函数 - 需要使用
mb_
对应函数。然而,几乎每个 PHP 安装都应该启用此扩展。C
means "use whatever locale is hard coded" (and since most *NIX programs are written in C, it's calledC
). However, it is usually not an UTF-8 locale.If you are using multibyte charsets such as UTF-8 you cannot use the regular string functions - using the
mb_
counterparts is required. However, almost every PHP installation should have this extension enabled.