Ways to compare strings in PHP

Source: Internet
Author: User
Tags first string

by byte comparison

Comparing strings by Byte is the most common method. The functions that may be used are strcmp () and strcasecmp ().

The difference between these two functions is strcmp () that distinguishes the case of characters, strcasecmp () does not differentiate between the case of characters, and the use of both is basically the same.

Only strcmp () is introduced here;

The syntax is as follows

int strcmp (string str1,string str2)

The parameter str1 and parameter str2 are two strings to compare, 0 if equal, if the parameter str1 is greater than str2, the return value is greater than 0, and the return value is less than 0 if the parameter str1 is less than str2.

For example:

$str 1="107website Studio";
$str 2="107website Work";
Echostrcmp($str 1,$str 2);
$STR 3="Lab";
$STR 4="LAB";
Echostrcmp($STR 3,$STR 4);
Echostrcasecmp($STR 3,$STR 4);

The results of the operation are as follows:

310

Comparison by natural sorting method

In PHP, string comparisons are implemented by the same strnatcmp () function according to the natural sorting method. The natural sorting method compares the numeric parts of a string, sorting the numbers in the string by the size of the rows.

The syntax is as follows:

int strnatcmp (string str1,string str2)

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

In natural algorithms, the number 2 is less than the number 10. In computer sequencing, 10 is less than 2, because the first number in 10 is less than 2.

$str 1="Str3.jpg";
$str 2="Str10.jpg";
Echo"by byte comparison:".strcmp($str 1,$str 2)."<br>";
Echo"Compare by natural sorting method:".strnatcmp($str 1,$str 2)."<br>";
$STR 3="MRSOFT1";
$STR 4="MRSOFT2";
Echo"by byte comparison:".strcmp($STR 3,$STR 4)."<br>";
Echo"Compare by natural sorting method:".strnatcmp($STR 3,$STR 4)."<br>";

The output is:

Comparison by byte: 1
Comparison by natural sorting method: 1
Comparison by byte: 1
Comparison by natural sorting method: 1

$str 1 = "MRSOFT1"; $str 2 = "MRSOFT2"; Echo strnatcmp ($str 1, $str 2); 1 because M is greater than M$STR1 = "MRSOFT1"; $str 2 = "mrsoft2"; Echo strnatcmp ($str 1, $str 2); -1 to $str1 = "mrsoft11"; $str 2 = "mrsoft2"; Echo strnatcmp ($str 1, $str 2); 1 That's what it meant. 11 Greater than 2

Description: When comparing by natural sort, there is also a function identical to the STRNATCMP () function, but the case-insensitive strnatcasecmp () function

Specifies the location comparison from the source string
The strncmp () function is used to compare the first n characters in a string, and the function is case-sensitive
The syntax is as follows:
int strncmp (string str1,string str2,int len)
The parameter str1 specifies the first string to compare. The parameter str2 specifies a second string to compare. Len (required) Specifies the number of characters for each string used in the comparison.
returns 0 if equal, or greater than 0 if the parameter str1 is greater than str2; If the parameter str1 is less than str2, the return value is less than 0. 
For example:
The result of the operation is
-1

tip: This function is similar to the strcmp () function, but the difference is that strcmp () has no length parameter

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.