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 |
|
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 |
|
The output is:
1 |
|
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 |
|
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 |
|
The resulting output is:
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 |
|
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 |
|
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 .