Current Location: Home> Latest Articles> How to sort strings with special characters using strnatcasecmp?

How to sort strings with special characters using strnatcasecmp?

gitbox 2025-05-19

In PHP, strnatcasecmp() is a function for string comparison, which can be compared in the natural order of strings. This means that it is not only compared by the ASCII value of the character, but also considers the size relationship of the numbers. Therefore, it is particularly useful for strings containing numbers, enabling a more natural way of sorting in humans. This function ignores the difference in case when sorting.

However, when dealing with strings with special characters, how to ensure the correctness of the sort is also a concern for many developers. In this article, we will discuss how to use the strnatcasecmp() function to sort strings with special characters naturally and make sure the sorted results are as expected.

1. What is the strnatcasecmp function?

The strnatcasecmp() function is used to compare the sizes of two strings, sort the strings in "natural order", and ignore the case of letters. Unlike the traditional strcmp() function, strcmp() will compare characters by character, while strnatcasecmp() will first extract the numbers in the string for comparison. Therefore, when processing strings containing numbers, they can sort them in the order of numbers.

Function definition:

 int strnatcasecmp ( string $str1 , string $str2 )
  • $str1 : The first string

  • $str2 : The second string

  • Return value : If $str1 is less than $str2 , return a negative number; if $str1 is equal to $str2 , return a zero; if $str1 is greater than $str2 , return a positive number.

2. How to sort using strnatcasecmp function?

Example:

 <?php
$str1 = "file2.txt";
$str2 = "file10.txt";
$str3 = "file1.txt";

$arr = [$str1, $str2, $str3];
usort($arr, 'strnatcasecmp');
print_r($arr);
?>

Output result:

 Array
(
    [0] => file1.txt
    [1] => file2.txt
    [2] => file10.txt
)

As shown in the above example, the strnatcasecmp() function makes the numbers sort in natural order of humans, rather than based on the ASCII value of the letter. This is especially useful for file names or any string containing numbers.

3. Process strings with special characters

Stranatcasecmp() can also be handled correctly for strings containing special characters (such as symbols, spaces, etc.). However, you need to be aware of the position and type of special characters in the string, especially when string sorting, they may affect the sorting result.

Example:

 <?php
$str1 = "file-2.txt";
$str2 = "file-10.txt";
$str3 = "file-1.txt";
$str4 = "[email protected]";

$arr = [$str1, $str2, $str3, $str4];
usort($arr, 'strnatcasecmp');
print_r($arr);
?>

Output result:

 Array
(
    [0] => [email protected]
    [1] => file-1.txt
    [2] => file-2.txt
    [3] => file-10.txt
)

In this example, special characters (such as @ and - ) affect the order when string sorting. As can be seen, the string with @ is in front. PHP determines the order of these characters based on ASCII values ​​when comparing, so you can adjust the position or priority of special characters with appropriate preprocessing.

4. How to replace the domain name in the URL?

If the string contains a URL and you want to replace its domain name with a specific domain name (for example, replace all domain names in the URL with gitbox.net ), you can use regular expressions to handle this. Here is an example:

Sample code:

 <?php
function replaceDomain($url) {
    return preg_replace('/https?:\/\/[^\/]+/', 'https://gitbox.net', $url);
}

$str1 = "https://example.com/page1";
$str2 = "https://testsite.org/hello";
$str3 = "https://mywebsite.net/about";

$arr = [$str1, $str2, $str3];
$arr = array_map('replaceDomain', $arr);

// use strnatcasecmp Do natural sorting
usort($arr, 'strnatcasecmp');
print_r($arr);
?>

Output result:

 Array
(
    [0] => https://gitbox.net/page1
    [1] => https://gitbox.net/hello
    [2] => https://gitbox.net/about
)

In this example, all domain names in the URL are replaced with gitbox.net and then sorted using strnatcasecmp() . Regular expressions allow you to have the flexibility to process and replace any part of a string.

5. Summary

Using the strnatcasecmp() function makes it very convenient to sort strings with special characters naturally. Whether strings contain numbers, symbols, or special characters, strnatcasecmp() can correctly compare and sort in natural order. If you need to replace the URL domain, you can use a combination of regular expressions and string replacement functions to complete the task.

Hope this article helps you when dealing with string sorting that contains special characters and URLs!