In PHP, the strnatcmp () function "natural sorting algorithm" is used to analyze the usage of string comparison (compared with the strcmp function) and strnatcmpstrcmp. In PHP, the strnatcmp () function "natural sorting algorithm" is used to analyze the usage of string comparison (compared with the strcmp function). This document describes the strnatcmp () in PHP () use the strnatcmp () function in "natural sorting algorithm" in PHP to analyze the usage of string comparison (compared with the strcmp function) and strnatcmpstrcmp.
This article describes how to use strnatcmp () function "natural sorting algorithm" in PHP to compare strings. We will share this with you for your reference. The details are as follows:
PHPThe strnatcmp () function uses the "natural" algorithm to compare two strings (case sensitive). generally, in the natural algorithm, number 2 is smaller than Number 10. In computer sorting, 10 is less than 2, because the first number in 10 is less than 2..
The strnatcmp () function is defined as follows:
Strnatcmp (string1, string2)
Parameter description:
String1 is required. Specifies the first string to be compared.
String2 is required. Specifies the second string to be compared.
Return value description:
If the two strings are equal, the return value is 0.
If string1 is less than string2, the return value is less than 0.
If string1 is greater than string2, the return value is greater than 0.
Sample codeAs follows:
<? Php $ str1 = "2.jpg"; $ str2 =" 10.jpg"; $ str3 = "jb51.net _ 1"; $ str4 = "JB51.NET _ 2"; echo strcmp ($ str1, $ str2); // compare by byte, returns 1 echo"
"; Echo strcmp ($ str3, $ str4); // compare by byte, return 1 echo"
"; Echo strnatcmp ($ str1, $ str2); // compare by" natural sorting ", return-1 echo"
"; Echo strnatcmp ($ str3, $ str4); // compare by" natural sorting ", returns 1?>
The running result is as follows:
11-11