- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
PHP strtolower()
函数应该将字符串转换为小写。但是,它说 in the PHP Manual (强调):
Returns string with all alphabetic characters converted to lowercase.
Note that 'alphabetic' is determined by the current locale. This means that in i.e. the default "C" locale, characters such as umlaut-A (Ä) will not be converted.
手册在这里没有提及编码,但众所周知 strtolower()
会损坏 UTF-8 字符串,您应该使用 mb_strtolower()
代替.
我正在寻找解决方案,以应对 mbstring
扩展不可用的情况,并且想知道何时可以安全地使用 strtolower()
。
感谢评论这个问题的人给我的指示,PHP 源代码的相关部分似乎是调用 ctype.h 中的
库。 library documentation说(强调):tolower()
函数
If the argument of tolower() represents an uppercase letter, and there exists a corresponding lowercase letter (as defined by character type information in the program locale category LC_CTYPE ), the result shall be the corresponding lowercase letter.
根据我的测试,在带有 set_locale( LC_CTYPE, 'C' );
的 PHP 中,诸如 Ä
(编码为 ISO-8859-1)的字符保持不变.但在其他一些语言环境中,该函数返回小写的 ä
(同样,在 ISO-8859-1 中)。无论如何,将语言环境更改为使用 UTF-8 字符集的语言环境不会使 PHP strtolower()
可以处理 UTF-8 字符 Ä
.
考虑到越来越多的 I18N 相关问题和多语言环境,此信息可能至关重要。许多应用程序依赖于 strtolower()
进行简单的不区分大小写的检查。考虑:
$_POST['username'] = 'Michèlle';
if ( strtolower( $_POST['username'] ) == $database['username'] ) ...
现在,根据编码、语言环境和可能的其他一些变量,上面的代码将在某些环境中工作,但在其他环境中不工作。
问题是:鉴于 PHP strtolower()
函数使用 ctype.h
库的 tolower
函数,这取决于“程序locale category”,什么时候可以安全地依赖这个函数?在以下情况下可以指望该行为吗?
(编辑:问题已于 2013 年 11 月 26 日完全改写。)
最佳答案
strtolower()
PHP 函数在其实现中确实使用了 tolower()
C 函数,该函数对传递的字符串参数的每个字节(八位字节)进行操作。
这就是为什么 setlocale(LC_CTYPE, 'C' );
不会破坏 UTF-8 编码字符串的原因,因为它不会改变大于 127 的字节。也就是说,它只会改变大小写US-ASCII 字符 A-Z。
“C
”区域设置是默认设置的,您不需要使用 setlocale()
显式设置它,只有在应用程序的其他部分已经设置它的情况下到不同的值。
这也解释了为什么将 LC_CTYPE
设置为像“de_DE.UTF-8
”这样的 UTF8 语言环境不会将“Ä
”转换为“ä
":该字母用两个字节 0xC3 0x84 编码,其中两个字节都作为单个字符(八位字节)传递给 tolower()
C 函数 - 因此它们没有变化对于单个字节,UTF-8 到较低的处理只能处理 < 128 的字符,这又是有效的 A-Z。这实际上类似于 C 语言环境。
因此将 LC_CTYPE
设置为“C
”可防止破坏 strtolower()
使用的 UTF-8 字符串。
关于php - 什么时候使用 PHP strtolower() 函数是安全的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20101371/
C语言sscanf()函数:从字符串中读取指定格式的数据 头文件: ?
最近,我有一个关于工作预评估的问题,即使查询了每个功能的工作原理,我也不知道如何解决。这是一个伪代码。 下面是一个名为foo()的函数,该函数将被传递一个值并返回一个值。如果将以下值传递给foo函数,
CStr 函数 返回表达式,该表达式已被转换为 String 子类型的 Variant。 CStr(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CSng 函数 返回表达式,该表达式已被转换为 Single 子类型的 Variant。 CSng(expression) expression 参数是任意有效的表达式。 说明 通常,可
CreateObject 函数 创建并返回对 Automation 对象的引用。 CreateObject(servername.typename [, location]) 参数 serv
Cos 函数 返回某个角的余弦值。 Cos(number) number 参数可以是任何将某个角表示为弧度的有效数值表达式。 说明 Cos 函数取某个角并返回直角三角形两边的比值。此比值是
CLng 函数 返回表达式,此表达式已被转换为 Long 子类型的 Variant。 CLng(expression) expression 参数是任意有效的表达式。 说明 通常,您可以使
CInt 函数 返回表达式,此表达式已被转换为 Integer 子类型的 Variant。 CInt(expression) expression 参数是任意有效的表达式。 说明 通常,可
Chr 函数 返回与指定的 ANSI 字符代码相对应的字符。 Chr(charcode) charcode 参数是可以标识字符的数字。 说明 从 0 到 31 的数字表示标准的不可打印的
CDbl 函数 返回表达式,此表达式已被转换为 Double 子类型的 Variant。 CDbl(expression) expression 参数是任意有效的表达式。 说明 通常,您可
CDate 函数 返回表达式,此表达式已被转换为 Date 子类型的 Variant。 CDate(date) date 参数是任意有效的日期表达式。 说明 IsDate 函数用于判断 d
CCur 函数 返回表达式,此表达式已被转换为 Currency 子类型的 Variant。 CCur(expression) expression 参数是任意有效的表达式。 说明 通常,
CByte 函数 返回表达式,此表达式已被转换为 Byte 子类型的 Variant。 CByte(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CBool 函数 返回表达式,此表达式已转换为 Boolean 子类型的 Variant。 CBool(expression) expression 是任意有效的表达式。 说明 如果 ex
Atn 函数 返回数值的反正切值。 Atn(number) number 参数可以是任意有效的数值表达式。 说明 Atn 函数计算直角三角形两个边的比值 (number) 并返回对应角的弧
Asc 函数 返回与字符串的第一个字母对应的 ANSI 字符代码。 Asc(string) string 参数是任意有效的字符串表达式。如果 string 参数未包含字符,则将发生运行时错误。
Array 函数 返回包含数组的 Variant。 Array(arglist) arglist 参数是赋给包含在 Variant 中的数组元素的值的列表(用逗号分隔)。如果没有指定此参数,则
Abs 函数 返回数字的绝对值。 Abs(number) number 参数可以是任意有效的数值表达式。如果 number 包含 Null,则返回 Null;如果是未初始化变量,则返回 0。
FormatPercent 函数 返回表达式,此表达式已被格式化为尾随有 % 符号的百分比(乘以 100 )。 FormatPercent(expression[,NumDigitsAfterD
FormatNumber 函数 返回表达式,此表达式已被格式化为数值。 FormatNumber( expression [,NumDigitsAfterDecimal [,Inc
我是一名优秀的程序员,十分优秀!