The strnatcasecmp Function
The strnatcasecmp
function performs a comparison of two strings case-insensitively,
using a natural ordering algorithm (natural order). The first parameter
passes the first string for comparison, the second - the second string. The function returns 0
if the strings are identical, a number -1
if the first string is less than the second,
and a number greater than 1
if the first string is greater than the second.
Syntax
strnatcasecmp(string1, string2);
Example
Let's compare two identical strings case-insensitively:
<?php
$res = strnatcasecmp('Hello', 'hello');
echo $res;
?>
Code execution result:
0
Example
Let's compare strings with numbers using natural ordering:
<?php
$res = strnatcasecmp('file2.txt', 'file10.txt');
echo $res;
?>
Code execution result:
-1
Example
Comparing strings with different character cases:
<?php
$res = strnatcasecmp('Apple', 'banana');
echo $res;
?>
Code execution result:
-1
See Also
-
the
strnatcmp
function,
which compares strings case-sensitively -
the
strcasecmp
function,
which compares strings case-insensitively -
the
strcmp
function,
which compares strings case-sensitively