Current Location: Home> Latest Articles> How to sort strings of multilingual character sets using strnatcasecmp?

How to sort strings of multilingual character sets using strnatcasecmp?

gitbox 2025-05-19

When dealing with multilingual character sets, we often need to sort strings. Ordinary string sorting is usually based on alphabetical order, but natural sorting can sort numbers and letters according to human habit. The strnatcasecmp function in PHP is to solve this problem. It can naturally sort strings without considering case.

What is natural sorting?

Natural sorting is a sorting method in which numbers are sorted by their values ​​rather than compared one by one by one by one by one by one. For example:

  • "item1" will be ahead of "item12"

  • "item2" will be ahead of "item21"

This sorting method is especially suitable for strings containing numbers, such as file names, version numbers, product numbers, etc.

Introduction to strnatcasecmp function

PHP's strnatcasecmp function is used to compare two strings in natural order. It is similar to the strcasecmp function, the difference is that strnatcasecmp adopts natural sorting rules and is case-insensitive.

Function prototype:

 int strnatcasecmp ( string $str1 , string $str2 )
  • $str1 and $str2 are two strings to be compared.

  • Return value: If $str1 is less than $str2 , a negative integer is returned; if equal, a 0 return; if $str1 is greater than $str2 , a positive integer is returned.

How to sort multilingual character sets using strnatcasecmp ?

For multilingual strings, strnatcasecmp can still be sorted naturally. However, the sorting rules may vary between different character sets. In PHP, strnatcasecmp is sorted by UTF-8 encoding by default, so it can handle strings of multilingual character sets.

For example, if we want to sort a naturally in arrays containing Chinese, English, and other characters, we can do it as follows:

Example: Natural sorting using strnatcasecmp

 <?php

// Array of strings to be sorted
$array = [
    'document3.txt',
    'document1.txt',
    'document2.txt',
    'File10.txt',
    'File2.txt',
    'document11.txt'
];

// Custom sorting functions
usort($array, function($a, $b) {
    return strnatcasecmp($a, $b);
});

// Output sorted array
print_r($array);

?>

Output result:

 Array
(
    [0] => document1.txt
    [1] => document2.txt
    [2] => document3.txt
    [3] => File2.txt
    [4] => File10.txt
    [5] => document11.txt
)

In this example, we use the usort function to sort an array containing multiple character sets. By providing a custom comparison function strnatcasecmp , we implement natural sorting of file names. You can see that the number sorting is in line with human habits, with file 10.txt ranking behind file 2.txt , and File2.txt ranking before File10.txt .

Replace the domain name in the URL

If URL addresses are involved in the application and those URLs contain domain names, you may need to replace the domain name section. We can do this through PHP's string processing function. Here is an example showing how to replace a domain name in a URL with gitbox.net .

 <?php

// To be replaced URL
$url = "https://www.example.com/path/to/resource";

// Replace domain name
$new_url = preg_replace('/^https?:\/\/[^\/]+/', 'https://gitbox.net', $url);

// Output new URL
echo $new_url;  // Output:https://gitbox.net/path/to/resource

?>

Summarize

The strnatcasecmp function in PHP is a very useful tool, especially when dealing with strings of multilingual character sets. It ensures that strings are compared according to the rules of natural sorting, and there will be no wrong sorting caused by numerical ordering problems in traditional sorting. In addition, with the help of string processing functions, we can also conveniently replace the domain names in the URL to meet different needs.

Hope this article helps you better understand how to sort naturally in PHP and how to handle URLs. If you have any questions or further requirements, feel free to ask questions!