PHP strnatcmp () function "Natural Sorting algorithm" for string comparison usage analysis (contrast strcmp function) _php tips

Source: Internet
Author: User
Tags first string strcmp

This article describes the STRNATCMP () function "Natural sorting algorithm" in PHP for string comparison usage. Share to everyone for your reference, specific as follows:

The strnatcmp () function in PHP uses the "natural" algorithm to compare two strings (case-sensitive), usually in the natural algorithm, where the number 2 is less than the number 10. In the computer sort, 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. Specify the first string to compare.
string2 required. Specify the second string to compare.

Return Value Description:

If 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
 $str 1= "2.jpg";
 $str 2= "10.jpg";
 $str 3= "jb51.net_1";
 $str 4= "Jb51.net_2";
 Echo strcmp ($str 1, $str 2);//By Byte comparison, returns 1
 echo "<br/>";
 Echo strcmp ($str 3, $str 4);//By Byte comparison, return 1
 echo "<br/>";
 Echo strnatcmp ($str 1, $str 2);//Compare by "natural order" method, return-1
 echo "<br/>";
 Echo strnatcmp ($str 3, $str 4);//Compare by "natural sort" method, return 1
?>

The results of the operation are as follows:

1
1
-1
1

More about PHP string Operations related to the site to see the topic: "PHP string (String) Usage Summary"

I hope this article will help you with the PHP program design.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.