strnatcmp区分字母大小写php,PHP strnatcmp 用法 手册 | 示例代码

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 20:25   1062   0

This function has some interesting behaviour on strings consisting of mixed numbers and letters.

One may expect that such a mixed string would be treated as alpha-numeric, but that is not true.

var_dump(strnatcmp('23','123')); →

int(-1)

As expected, 23<123 (even though first digit is higher, overall number is smaller)

var_dump(strnatcmp('yz','xyz')); →

int(1)

As expected, yz>xyz (string comparison, irregardless of string length)

var_dump(strnatcmp('2x','12y')); →

int(-1)

Remarkable, 2x<12y (does a numeric comparison)

var_dump(strnatcmp('20x','12y'));

int(1)

Remarkable, 20x>12y (does a numeric comparison)

It seems to be splitting what is being compared into runs of numbers and letters, and then comparing each run in isolation, until it has an ordering difference.

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP