In many scenarios, we need to sort by filename. Common sorting methods such as dictionary sorting may lead to unexpected results. For example, sort by dictionary, file2.txt will be ranked before file10.txt because "2" is smaller than "1". However, users usually want the file names to be sorted in numerical order, i.e. file2.txt should be placed after file10.txt , which is called "natural sorting".
The strnatcasecmp function in PHP is designed to meet this requirement. It can compare two strings and sort them according to natural sorting rules. strnatcasecmp is a "case-insensitive" natural sorting method that is very suitable for file name sorting.
Natural sorting is based on human intuitive sorting. For example, in the case of file names file1 , file2 , file10 , the result of sorting in nature should be:
file1
file2
file10
Instead of dictionary sorting results:
file1
file10
file2
strnatcasecmp is a very useful function in PHP that compares two strings and compares them according to natural sorting rules. The syntax of this function is as follows:
int strnatcasecmp ( string $string1 , string $string2 )
$string1 and $string2 are two strings to compare.
Function return value:
If $string1 is less than $string2 , a negative integer is returned.
If $string1 is equal to $string2 , return 0.
If $string1 is greater than $string2 , a positive integer is returned.
Through this function, we can easily implement the natural sorting of file names. Here is an example that demonstrates how to sort file names using strnatcasecmp .
Suppose we have the following file name:
$files = ['file10.txt', 'file2.txt', 'file1.txt', 'file20.txt', 'file11.txt'];
We want to sort these file names naturally so that the file names are arranged in numerical order. We can use strnatcasecmp to achieve:
<?php
$files = ['file10.txt', 'file2.txt', 'file1.txt', 'file20.txt', 'file11.txt'];
// use usort and strnatcasecmp Do natural sorting
usort($files, 'strnatcasecmp');
// Output the sorted file name
print_r($files);
?>
We define an array of filenames $files .
Use the usort function to sort the array, which accepts an array and a callback function as parameters. Here we pass strnatcasecmp as a callback function into usort .
After sorting is completed, the file names in the $files array will be arranged according to the rules of natural sorting.
Array
(
[0] => file1.txt
[1] => file2.txt
[2] => file10.txt
[3] => file11.txt
[4] => file20.txt
)
As you can see, the file names are already sorted in the order of numbers. In this way, file2.txt is ranked ahead of file10.txt .
strnatcmp and strnatcasecmp are both functions used in PHP for natural sorting. The difference between the two is:
strnatcmp is case sensitive;
strnatcasecmp is case-insensitive.
If you want to ignore case differences in file names, you can use strnatcasecmp . Otherwise, you can choose to use strnatcmp .
When sorting using ordinary string comparison functions such as strcmp , you may encounter problems like "file2.txt" and "file10.txt", and the results will not be arranged in intuitive numerical order. strnatcasecmp can ensure that strings are compared in numerical order, which is more in line with users' expectations, especially when it comes to file names, version numbers, etc. that require natural sorting.