Current Location: Home> Latest Articles> How to use strnatcasecmp to sort each line naturally in a multi-line text?

How to use strnatcasecmp to sort each line naturally in a multi-line text?

gitbox 2025-05-27

In PHP, the strnatcasecmp function is a function used to perform natural order comparisons. It compares strings to take into account the numbers in the string, sorting in the size of the numbers, rather than in dictionary order. This is especially useful for processing text with numbers, such as sorting file names, list items, etc. with numbers.

This article will explain how to use PHP's strnatcasecmp function to sort multiple lines of text naturally.

1. What is the strnatcasecmp function?

The strnatcasecmp function is used to compare two strings in a natural order, which ignores case and sorts in natural order according to the numbers in the string. This makes it more precise than the regular strcmp function when dealing with text with numbers.

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

  • The return value is an integer: if str1 is less than str2 , it returns a negative value; if str1 is greater than str2 , it returns a positive value; if they are equal, it returns 0.

2. Example: How to sort multiple lines of text naturally?

Suppose we have the following lines of text that need to be sorted according to natural sorting. The sample text includes some lines with numbers:

 line10
line2
line1
line20
line11
line3

We want these lines of text by numerical size, not alphabetical order.

3. Sort multiline text using strnatcasecmp

We can first store these texts in an array and then sort them using the usort function with strnatcasecmp . Here is the complete sample code:

 <?php
// Multi-line text array
$text = [
    'line10',
    'line2',
    'line1',
    'line20',
    'line11',
    'line3'
];

// use strnatcasecmp Sort arrays
usort($text, 'strnatcasecmp');

// Output sorted array
foreach ($text as $line) {
    echo $line . PHP_EOL;
}
?>

explain:

  • We use the usort function to sort the array $text . The usort function accepts two parameters, the first is an array, and the second is a comparison function for sorting. Here we use the strnatcasecmp function as a comparator.

  • strnatcasecmp will sort naturally according to the numbers in the string.

  • The sorted output will be:

 line1
line2
line3
line10
line11
line20

4. Process text containing URLs

When processing text that contains URLs, we may need to sort these text that contain domain names and paths. Assuming that the text contains multiple URLs, how can I use strnatcasecmp to sort them?

 <?php
// Include URL Text array
$urls = [
    'https://example.com/page10',
    'https://gitbox.net/page2',
    'https://gitbox.net/page1',
    'https://example.com/page20',
    'https://gitbox.net/page11',
    'https://example.com/page3'
];

// use strnatcasecmp right URL Arrays for sorting
usort($urls, 'strnatcasecmp');

// Output sorted URL
foreach ($urls as $url) {
    echo $url . PHP_EOL;
}
?>

Note: In the example above, we have replaced the domain name of all URLs with gitbox.net . This code is sorted naturally based on the path part in the URL (i.e. the numeric part after /page ).

5. Summary

The strnatcasecmp function is a very useful tool, especially when you need to sort strings containing numbers. By using usort with strnatcasecmp we can easily sort multiple lines of text or URLs naturally.

This sorting method not only sorts the numeric parts reasonably, but also avoids the sorting problems that traditional string comparison methods may bring. strnatcasecmp is a very practical function when dealing with text containing numbers.