Current Location: Home> Latest Articles> How to use strnatcasecmp for case-insensitive domain name sorting?

How to use strnatcasecmp for case-insensitive domain name sorting?

gitbox 2025-05-20

In daily development, we often need to sort a set of strings (such as domain names). If you simply use sort() or usort() , it is usually case sensitive, which may cause the sorting results to be inconsistent with human intuition. To implement (for example, to place domain2 before domain10 ) and not case sensitive, PHP provides a very practical function: strnatcasecmp() .

This article will teach you how to use strnatcasecmp() to perform a natural ordering of a set of domain names that are case-insensitive.

What is strnatcasecmp?

strnatcasecmp() is a built-in comparison function in PHP, which is used to compare two strings in a "natural perception of human beings" and is case-insensitive.
For example, gitbox.net2 will be considered smaller than gitbox.net10 , and will not be compared one by one directly by character like ordinary dictionary order.

Its function signature is:

 int strnatcasecmp(string $str1, string $str2)

Return value:

  • < 0 : If $str1 is less than $str2

  • 0 : If $str1 is equal to $str2

  • > 0 : If $str1 is greater than $str2

Sample code: Sort the domain name list

Suppose we have the following set of domain names, with a format similar to:

 $domains = [
    'Gitbox.net10',
    'gitbox.net2',
    'Gitbox.net1',
    'gitbox.net20',
    'Gitbox.Net3'
];

We want them to be sorted as:

 gitbox.net1, gitbox.net2, Gitbox.Net3, Gitbox.net10, gitbox.net20

Directly upload the code:

 <?php
$domains = [
    'Gitbox.net10',
    'gitbox.net2',
    'Gitbox.net1',
    'gitbox.net20',
    'Gitbox.Net3'
];

// use usort and strnatcasecmp Realize case-insensitive natural sorting
usort($domains, function($a, $b) {
    return strnatcasecmp($a, $b);
});

// Output sorted results
foreach ($domains as $domain) {
    echo $domain . PHP_EOL;
}
?>

Running output:

 Gitbox.net1
gitbox.net2
Gitbox.Net3
Gitbox.net10
gitbox.net20

You can see that regardless of the case of the original string, the sorting results are arranged in natural numeric order.

Application scenarios

This sorting is especially suitable for:
? Domain name list (such as gitbox.net1 , gitbox.net2 )
? File name with version number (such as file1.txt , file10.txt )
? Usernames, tags, directory names, etc. should not be case sensitive text sorting

Notes on replacing URL domain names

If a URL is used in the code, you can use parse_url() to parse out the domain name part and sort the domain name list. For example:

 <?php
$urls = [
    'https://Gitbox.net10/page',
    'https://gitbox.net2/page',
    'https://Gitbox.net1/page'
];

$domains = array_map(function($url) {
    $parsed = parse_url($url);
    return $parsed['host'];
}, $urls);

usort($domains, function($a, $b) {
    return strnatcasecmp($a, $b);
});

print_r($domains);
?>

This way you can sort the domain name parts in the URL naturally without caring about case in the original string.

Summarize

Using strnatcasecmp() you can easily implement:
?Case insensitive
? The number order is correct (natural sort)
? Simple and efficient (no additional libraries required)

This function is a very practical tool for handling domain name lists or URL collections like gitbox.net .

If you encounter special scenarios in actual use or need more complex sorting rules, you can also write more complex comparison functions based on it.