Phpstrnatcmp () uses natural algorithms to compare two strings

Source: Internet
Author: User
The phpstrnatcmp () function uses a natural algorithm to compare two strings. This article introduces the phpstrnatcmp () function usage and basic use examples to the coders. if you need them, refer to this article. Definition and usage

The strnatcmp () function uses a "natural" algorithm to compare two strings.

In natural algorithms, 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.

Note: This function is case sensitive.

Syntax
strnatcmp(string1,string2)
Parameters Description
String1 Required. Specifies the first string to be compared.
String2 Required. Specifies the second string to be compared.

Return value
Return value:

This function returns:

  • 0-if the two strings are equal
  • <0-ifString1LessString2
  • > 0-ifString1GreaterString2
PHP version: 4 +

Instance

Differences between natural algorithms (strnatcmp) and conventional computer string sorting algorithms (strcmp:


  "; Usort ($ arr1," strcmp "); print_r ($ arr1); echo"
"; Echo" natural sequence string comparison "."
"; Usort ($ arr2," strnatcmp "); print_r ($ arr2);?>

Online operation

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.