The example in this article describes the use of strnatcmp() function "natural sorting algorithm" in PHP for string comparison. Share it with everyone for your reference, the details are as follows:
The strnatcmp() function in PHP uses the "natural" algorithm to compare two strings (case-sensitive). Usually in the natural algorithm, the number 2 is less than the number 10. And in computer sorting, 10 is less than 2 because the first number in 10 is less than 2.
strnatcmp() function is defined as follows:
strnatcmp(string1,string2)
Parameter description:
string1 Required. Specifies the first string to compare.
string2 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
The sample code is as follows:
<?php $str1="2.jpg"; $str2="10.jpg"; $str3="jb51.net_1"; $str4="JB51.NET_2"; echo strcmp($str1,$str2);//按字节进行比较,返回1 echo "<br/>"; echo strcmp($str3,$str4);//按字节进行比较,返1 echo "<br/>"; echo strnatcmp($str1,$str2);//按"自然排序"法进行比较,返回-1 echo "<br/>"; echo strnatcmp($str3,$str4);//按"自然排序"法进行比较,返回1 ?>
The running results are as follows:
1 1 -1 1
For more information about PHP string operations, please view the special topic of this site: "Summary of php string (string) usage"
I hope this article will help everyone in PHP programming Helps.
The above has introduced the strnatcmp function "Natural Sorting Algorithm" in PHP for string comparison usage analysis and comparison of the strcmp function, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.