String comparison usage analysis of strnatcmp() function 'Natural Sorting Algorithm' in PHP (compared with strcmp function), strnatcmpstrcmp_PHP tutorial

WBOY
Release: 2016-07-12 09:01:18
Original
675 people have browsed it

Strnatcmp() function "Natural Sorting Algorithm" in PHP performs string comparison usage analysis (compare strcmp function), strnatcmpstrcmp

This article describes the strnatcmp() function in PHP" Natural sorting algorithm" for string comparison usage. 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.

The 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

Sample code

is as follows:

The running results are as follows:
<&#63;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
&#63;>

Copy after login

For more information about
1
1
-1
1

Copy after login

PHP string operations , please see the special topic on this site: "Summary of PHP String Usage" I hope this article will be helpful to everyone in PHP programming.

Articles you may be interested in:

How to obtain the number of occurrences of a substring with the substr_count() function in PHP
  • After PHP uses the strstr() function to obtain the specified string Methods for all characters
  • Strncmp() function in PHP compares whether the first 2 characters of two strings are equal
  • Analysis of string comparison usage of strcmp() and strcasecmp() functions in PHP
  • Analysis on usage of substr function string interception in PHP
  • Example of PHP using trim function to remove left and right spaces and special characters from a string
  • PHP encapsulated string encryption and decryption function
  • The most accurate PHP string length interception function
  • What to do if the problem of Chinese garbled characters occurs when substr() is used to intercept strings in PHP
  • A summary of examples of common string manipulation functions in PHP

http://www.bkjia.com/PHPjc/1089577.html

truehttp: //www.bkjia.com/PHPjc/1089577.htmlTechArticleStrnatcmp() function "Natural Sorting Algorithm" in PHP performs string comparison usage analysis (compare strcmp function), strnatcmpstrcmp This article describes the "natural sorting algorithm" of the strnatcmp() function in PHP...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!