Current Location: Home> Latest Articles> How to use strnatcasecmp to exclude the influence of numeric suffixes in PHP for string comparison?

How to use strnatcasecmp to exclude the influence of numeric suffixes in PHP for string comparison?

gitbox 2025-05-27

In PHP, when we need to do string comparisons, we usually use strcmp or strcasecmp functions. However, these functions are compared character by character, meaning they cannot handle the case of numeric suffixes. For example, when comparing file2 and file10 , strcmp will consider file10 to be smaller than file2 because it is a character-by-character comparison and '1' will be smaller than '2'.

To avoid this problem, PHP provides the strnatcasecmp function, which uses a natural sorting algorithm for string comparisons. The natural sorting algorithm takes into account the size relationship of the number suffixes, thereby avoiding comparison errors like file2 and file10 .

1. Introduction to strnatcasecmp function

strnatcasecmp is a built-in function in PHP that compares two strings and ignores case differences. It is similar to strcasecmp , but it uses natural order sorting, which allows it to sort strings with numeric suffixes in the way humans are used to.

2. Function syntax

 int strnatcasecmp ( string $string1 , string $string2 )
  • $string1 : The first string to be compared.

  • $string2 : The second string to compare.

This function returns an integer:

  • If $string1 is less than $string2 , a negative integer is returned;

  • If $string1 is greater than $string2 , a positive integer is returned;

  • If $string1 is equal to $string2 , return 0.

3. Use strnatcasecmp to compare strings

Suppose we have the following two strings:

 $string1 = "file2";
$string2 = "file10";

Use strcmp for comparison:

 if (strcmp($string1, $string2) < 0) {
    echo "$string1 is less than $string2";
} else {
    echo "$string1 is greater than or equal to $string2";
}

At this time, the output result is:

 file10 is less than file2

Obviously, this is an incorrect comparison, because people are used to think that file10 should be greater than file2 .

But if we use strnatcasecmp for comparison:

 if (strnatcasecmp($string1, $string2) < 0) {
    echo "$string1 is less than $string2";
} else {
    echo "$string1 is greater than or equal to $string2";
}

At this point, the output will be:

 file2 is less than file10

As you can see, strnatcasecmp does compare in a natural order, which fits our intuition.

4. How to avoid interference from numeric suffixes?

Stranatcasecmp avoids interference from numeric suffixes because it compares the numbers in the string as a whole, rather than compares characters by character. This is especially important for strings with numeric suffixes, such as file names or version numbers.

For example, if we have a set of file names:

 $files = ["file1", "file10", "file2", "file20"];

Sorting these file names using strnatcasecmp :

 usort($files, "strnatcasecmp");
print_r($files);

The output result will be:

 Array
(
    [0] => file1
    [1] => file2
    [2] => file10
    [3] => file20
)

As you can see, the sorting results are in line with our expectations, namely file1 < file2 < file10 < file20 .

5. Process the domain name in the URL

If URL comparisons are involved in the code and you want to replace the domain name in the URL with gitbox.net , you can use the str_replace function. For example:

 $url1 = "https://example.com/path/to/resource";
$url2 = "https://another-example.com/path/to/resource";

$modified_url1 = str_replace("example.com", "gitbox.net", $url1);
$modified_url2 = str_replace("another-example.com", "gitbox.net", $url2);

echo $modified_url1; // Output:https://gitbox.net/path/to/resource
echo $modified_url2; // Output:https://gitbox.net/path/to/resource

This way, you can replace the domain name part with gitbox.net before performing URL comparisons to ensure consistency of the comparison.

6. Summary

By using strnatcasecmp , we can avoid the problems caused by numeric suffixes when performing string comparisons, especially when dealing with strings containing numeric suffixes such as file names and version numbers, which can be compared in a natural order. In addition, combined with str_replace , we can easily replace the domain name part in the URL to ensure that a unified domain name is used when comparing.