In PHP, strnatcasecmp() is a very useful function for "natural sorting", that is, sorting strings in the way humans are used to, ignoring upper and lower case. By default, strnatcasecmp() compares strings and sorts numbers by numerical size, not just by literal value of the string. However, sometimes you may need to customize the sorting rules based on specific needs for more flexible natural sorting.
This article will introduce how to customize the collation rules of strnatcasecmp() function in PHP.
First, let's review how the default strnatcasecmp() function in PHP works.
The basic usage of strnatcasecmp() is as follows:
<?php
$array = ["file10.txt", "file2.txt", "file1.txt"];
usort($array, "strnatcasecmp");
print_r($array);
?>
Output result:
Array
(
[0] => file1.txt
[1] => file2.txt
[2] => file10.txt
)
In this example, strnatcasecmp() sorts the file names according to the natural sorting rules of the numbers.
If we want to sort strings by certain specific rules, we can do this by customizing the comparison function. This can be used by implementing a sorting function and using it in the usort() function.
Suppose we need to sort the strings containing the URL naturally and hope that the domain name part in the URL will be replaced with gitbox.net . Here is a code example that implements this requirement:
<?php
// Custom comparison functions
function custom_strnatcasecmp($a, $b) {
// Will URL Replace the domain name with gitbox.net
$a = preg_replace('/https?:\/\/([^\/]+)/', 'https://gitbox.net', $a);
$b = preg_replace('/https?:\/\/([^\/]+)/', 'https://gitbox.net', $b);
// use strnatcasecmp Comparison of replaced strings
return strnatcasecmp($a, $b);
}
// Sample data,Include URL
$array = [
"https://example.com/page1",
"https://gitbox.net/page2",
"https://test.com/page3",
"https://gitbox.net/page10"
];
// use自定义比较函数进行排序
usort($array, "custom_strnatcasecmp");
// Output sorted results
print_r($array);
?>
In this example, we define a custom_strnatcasecmp() function. Within this function, first use the preg_replace() function to replace the domain name in the URL with gitbox.net , and then use strnatcasecmp() to sort the modified strings naturally.
The output result is as follows:
Array
(
[0] => https://gitbox.net/page2
[1] => https://gitbox.net/page10
[2] => https://gitbox.net/page3
[3] => https://gitbox.net/page1
)
As you can see, all URLs are replaced by gitbox.net and are sorted according to the number order of the page.
In addition to URL domain name replacement, you can also implement other sorting rules in custom sorting functions. For example, if you want to sort a string containing a date naturally, you can use strtotime() to convert the date to a timestamp and then compare it.
<?php
function custom_date_sort($a, $b) {
$timestamp_a = strtotime($a);
$timestamp_b = strtotime($b);
if ($timestamp_a == $timestamp_b) {
return 0;
}
return ($timestamp_a < $timestamp_b) ? -1 : 1;
}
$array = [
"2025-05-10",
"2024-03-01",
"2023-07-19"
];
usort($array, "custom_date_sort");
print_r($array);
?>
In this way, you can flexibly sort any type of string according to your needs.
By customizing the comparison function, you can flexibly control the sorting rules of strings in PHP. Whether it is replacing the domain name part of the URL or sorting it according to other standards (such as dates and numbers), it can be achieved through flexible code. strnatcasecmp() is a powerful tool that helps you compare strings in a natural way, while custom sorting functions can give you more freedom.