note 121044 added to function.strnatcmp
| From: | chris at ocproducts dot com | Date: | Thu, 04 May 2017 01:16:48 +0000 |
| Subject: | note 121044 added to function.strnatcmp | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-209048@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 72.52.91.14
Probable Submitter: 86.3.44.227
----
Manual Page -- http://php.net/manual/en/function.strnatcmp.php
Edit -- https://master.php.net/note/edit/121044
Del: integrated -- https://master.php.net/note/delete/121044/integrated
Del: useless -- https://master.php.net/note/delete/121044/useless
Del: bad code -- https://master.php.net/note/delete/121044/bad+code
Del: spam -- https://master.php.net/note/delete/121044/spam
Del: non-english -- https://master.php.net/note/delete/121044/non-english
Del: in docs -- https://master.php.net/note/delete/121044/in+docs
Del: other reasons-- https://master.php.net/note/delete/121044
Reject -- https://master.php.net/note/reject/121044
Search -- https://master.php.net/manage/user-notes.php