PHP 8 reference · String Functions
PHP strnatcasecmp() Function
Compare strings in natural order ("a2" before "a10") while ignoring upper and lower case.
PHP 8 Since 4.0
Syntax
strnatcasecmp(string $string1, string $string2): int
Parameters
| Parameter | Description |
|---|---|
string1 |
The first string. |
string2 |
The second string. |
Return value
0 if equal; negative or positive otherwise.
Examples
<?php
var_dump(strnatcasecmp("Chapter10", "chapter9") > 0);
Output
bool(true)
<?php
$items = ["Item20", "item3", "ITEM100"];
usort($items, "strnatcasecmp");
print_r($items);
Output
Array
(
[0] => item3
[1] => Item20
[2] => ITEM100
)Version history
| Version | Change |
|---|---|
| PHP 4.0 | Added |
Related
strnatcmp()· Compare strings in natural order, so "img2" comes before "img10" the way people expect. Case-sensitive.natcasesort()· Sort an array in natural order while ignoring case, keeping keys. "Item2" comes before "item10".strcasecmp()· Compare two strings without caring about ASCII upper and lower case. Returns 0 if they match.- PHP Strings · tutorial