Detailed PHP string comparison function

In PHP, for comparison between the strings there are a variety of methods, using first strcmp () function and strcasecmp () function are compared by byte, using the second strnatcmp () function in a natural sort comparison, the third is to use a strncmp () function compares the designated start position of the source string. Here are several ways to conduct some of these in-depth explanation. Wuxi marble measurement platform

 

1. Compare strings in bytes

There are two methods according to the comparison byte string, respectively, using strcmp () function and strcasecmp () function. Difference between these two functions is strcmp () function is case sensitive characters, and strcasecmp () function does not distinguish between uppercase and lowercase characters. Since these two methods to achieve substantially the same functions, which here is just out strcmp () function to some introduction.

strcmp () function is used to compare two strings byte.

Syntax is as follows:

1

strcmp(string1, string2)

String1 string2 parameter specifies parameters and to compare two strings. If the function returns a value equal to 0; if the parameter is greater than the parameter string1 string2, the function returns a value greater than 0; if the parameter is less than the parameter string1 string2, the function returns a value less than 0.

Note that this function is case sensitive.

Use srtcmp () function and strcasecmp () function respectively two strings are compared byte, code examples are as follows:

1

2

3

4

5

6

7

8

9

<?php

 $str1 = "PHP学习手册!";               // 定义字符串常量

 $str2 = "PHP学习手册!";               // 定义字符串常量

 $str3 = "phpcn";                      // 定义字符串常量

 $str4 = "PHPCN";                      // 定义字符串常量

 echo strcmp($str1,$str2);             // 这两个字符串相等

 echo strcmp($str3,$str4);             // 注意该函数区分大小写

 echo strcasecmp($str3,$str4);          //该函数不区分大小写

?>

The output is:

1

0 1 0

Note: In PHP string between applications is very broad comparison. For example, using the strcmp () function compares the input of the user logs decency user name and password are correct, if not this function when validating user and password, then enter your user name and password either uppercase or lowercase, as long as the right to log. Use the srtcmp () after the function to avoid this situation, timely and correct, it must be the case all match before they can log in to improve the security of the site.

2. Compare strings according to the natural ordering method

In PHP, comparing strings according to the natural ordering method is achieved by using strnatcmp () function. Comparative sort admission number is part of the string, the number string is compared by size. Its syntax is the following format:

1

strnatcmp(string1, string2)

If the parameter is equal to string1 string2 parameter and the function returns a value of 0; if parameter string1 string2 greater than the parameter, the function returns a value greater than 0; if the parameter is less than string1 string2 parameter, the function returns a value less than 0.

Tip: This function is case sensitive.

Note: In the natural algorithm in 2 to 10 hours, however, the sequence of the computer, 2 is smaller than 10, because "10" in the first digit is "1", which is less than "2".

Use strnatcmp () function compares a string of natural ordering method according to the example code shown below:

1

2

3

4

5

6

7

8

<?php

 $str1 = "inter2.jpg";                  // 定义字符串常量

 $str2 = "inter10.jpg";                 // 定义字符串常量

 $str3 = "phpcn1";                      // 定义字符串常量

 $str4 = "PHPCN2";                      // 定义字符串常量

 echo strnatcmp($str1,$str2);           // 按自然排序法进行比较,返回-1

 echo strnatcmp($str3,$str4);           // 按自然排序法进行比较,返回1

?>

The resulting output is:

1

-1  1

Description: comparing admission according to sort, may also be used with other strnatcmp () function the same effect, but does not distinguish between strnatcasecmp size () function.

3. Specify the start position of the string from the source function of the comparison

a strncmp () function compares a string of first n characters.

Its syntax is the following format:

1

strncmp(string1, string2, length)

If the parameter is equal to string1 string2 parameter and the function returns a value of 0; if parameter string1 string2 greater than the parameter, the function returns a value greater than 0; if the parameter is less than string1 string2 parameter, the function returns a value less than 0. The case-sensitive function.

Its parameters are as follows:

parameter Explanation
string1 Specifies the first string to compare participation
string2 Specifies the second string to compare participation
length Necessary parameters, compare the number of strings to specify each string participation

Using a strncmp () function to compare the first two characters of the string is equal to the source string, the code shown in the following examples:

1

2

3

4

5

<?php

 $str1 = "I like this";                  // 定义字符串常量

 $str2 = "i study php";                  // 定义字符串常量

 echo strncmp($str1,$str2,2);            // 比较前两个字符

?>

The output is:

1

 

NOTE: It can be seen from the above code, the first letter of the string since the variables $ str2 to lowercase, does not match the variable $ str1 to the string, the function compares two strings after the return value is -1 .

Guess you like

Origin www.cnblogs.com/furuihua/p/12077139.html