Usage analysis of php character comparison functions similar_text, strnatcmp, and strcasecmp

Source: Internet
Author: User
This article mainly introduces the usage of php character comparison functions similar_text, strnatcmp, and strcasecmp, and analyzes in detail the usage of these three string comparison functions in the form of examples, which is of great practical value, for more information about the usage of php character comparison functions similar_text, strnatcmp, and strcasecmp, see the following example. Share it with you for your reference. The details are as follows:

① The similar_text () function calculates the number of matching characters for the Two Strings. This function can also calculate the similarity between the two strings, expressed in percentages.

Syntax: similar_text (string1, string2, percent)

Note: The levenshtein () function is faster than the similar_text () function. However, the similar_text () function provides more accurate results with fewer necessary modifications.

The code is as follows:

The code is as follows:

$ Str1 = "hello world"; // defines string 1
$ Str2 = "hello peter"; // defines string 2
$ Result = similar_text ($ str1, $ str2); // compare
Echo $ result; // output the comparison result.

② The strnatcmp () function uses a "natural" algorithm to compare two strings. in the natural algorithm, the number "2" is smaller than the number "10". in computer sorting, "2" is greater than "10", because "2" is greater than "10", the function returns:

0-if the two strings are equal, <0-if string1 is smaller than string2,> 0-if string1 is greater than string2.

Syntax: strnatcmp (string1, string2 ).

Note: This function is case sensitive.

The code is as follows:

$ Str1 = "hello world"; // defines string 1
$ Str2 = "hello world"; // defines string 2
$ Result = strnatcmp ($ str1, $ str2); // perform the comparison operation.
Echo $ result; // output the comparison result.

③ The strcasecmp () function compares two strings. This function returns:

0-if the two strings are equal, <0-if string1 is smaller than string2,> 0-if string1 is greater than string2.

Syntax: strcasecmp (string1, string2)

Note: This function is binary secure and case-insensitive. the code is as follows:

The code is as follows:

$ Str1 = "hello world"; // defines string 1
$ Str2 = "hello world"; // defines string 2
$ Result = strcasecmp ($ str1, $ str2); // perform the comparison operation.
Echo $ result; // output the comparison result.

I hope this article will help you with PHP programming.

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.