Summary of common php string comparison function instances

Source: Internet
Author: User
This article mainly introduces common php string comparison functions. examples summarize common functions such as substr_compare, strncasecmp, strncmp, and strcoll, which has some reference value, you can refer to the examples in this article to summarize common php string comparison functions. Share it with you for your reference. The specific analysis is as follows:

The substr_compare () function compares two strings from the specified start length, This function returns:

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

Syntax: substr_compare (string1, string2, startpos, length, case). The code is as follows:

The code is as follows:

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


The strnatcasecmp () function uses a "natural" algorithm to compare two strings.In natural algorithms, numbers "2" less than numbers "10", in computer sorting, "2" greater than "10 ", this is because the first digit "2" is greater than "10", the code is as follows:

The code is as follows:

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


The strncasecmp () 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: strncasecmp (string1, string2, length). The code is as follows:

The code is as follows:

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


The strncmp () 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: strncmp (string1, string2, length). The code is as follows:

The code is as follows:

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


The strcoll () 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.

The string comparison varies according to the local settings, aa.

Syntax: strcoll (string1, string2). The code is as follows:

The code is as follows:

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

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.