Current Location: Home> Latest Articles> How to combine strnatcasecmp and strtr to implement string conversion and natural sorting?

How to combine strnatcasecmp and strtr to implement string conversion and natural sorting?

gitbox 2025-05-19

In PHP, strnatcasecmp and strtr are two very useful functions, the former is used for comparison of natural sorting, and the latter is used for substitution operations of strings. If you need to sort naturally after string replacement, combining these two functions will be very effective.

This article will show how to use strtr to replace characters in a string and sort naturally using strnatcasecmp after replacement.

1. Understand the strnatcasecmp function

The strnatcasecmp function is a function used in PHP to perform natural sorting comparisons. It compares differently from the traditional dictionary order. It takes into account the order of numbers, not just in character order. For example, "10" will be ahead of "2" because in natural order, "10" is larger than "2".

Function prototype:

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

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

2. Understand strtr function

The strtr function is used to replace characters in a string. It accepts two parameters:

  • The first parameter is the target string that needs to be replaced.

  • The second parameter is a character map, which specifies which characters are to be replaced with other characters.

Function prototype:

 string strtr ( string $str , string $from , string $to )

For example, we can use strtr to replace letters in a string with other letters.

3. Use strtr and strnatcasecmp in combination

Suppose we have a set of strings containing file names that contain different characters and numbers. If we want to sort naturally after replacing certain characters, we can use the strtr function to replace the target characters first, and then use the strnatcasecmp function to sort.

Here is an example:

 <?php
// Array of strings to sort
$files = [
    'file10.txt',
    'file2.txt',
    'file1.txt',
    'file20.txt',
];

// Define character replacement rules
$trans = ['f' => 'F', 'e' => 'E'];

// use strtr Perform character replacement
$transformed_files = array_map(function($file) use ($trans) {
    return strtr($file, $trans);
}, $files);

// use strnatcasecmp Naturally sort the replaced strings
usort($transformed_files, 'strnatcasecmp');

// Output sorted results
print_r($transformed_files);
?>

4. Explain the code

  1. Define the array of strings to be sorted : We first define an array $files containing file names.

  2. Character replacement rules : We define character replacement rules through strtr , for example, replace the lowercase letter f in a string with the uppercase letter F and replace the letter e with the uppercase letter E.

  3. Apply replacement : We use the array_map function to apply strtr to each file name to get the replaced file name array.

  4. Natural sorting : Sort the replaced filename array by usort and strnatcasecmp so that it is arranged according to natural sorting rules.

  5. Output result : Finally, we output the sorted filename array through print_r .

5. Conclusion

By combining strnatcasecmp and strtr , we are able to maintain the natural order of sorting after replacing the characters in the string. This method is very useful when dealing with file names, version numbers, and other strings that require natural sorting. Hopefully this article helps you better understand how to use these two functions in PHP to handle string replacement and sorting.