This article mainly introduces the use summary of PHP string comparison functions strcmp () and strcasecmp (). This article introduces them in detail and provides specific examples, for more information about how to compare strings, it is one of the important features of string processing in any programming language. In PHP, besides the comparison operator (=, <或> In addition to comparison, a series of comparison functions are provided to allow PHP to perform more complex string comparison. Such as strcmp (), strcasecmp (), and strnatcmp.
1. string comparison in byte order
To compare strings in byte order, you can use the strcmp () and strcasecmp () functions. the strcasecmp () function can ignore the uppercase and lowercase letters in the string for comparison. The two functions are prototype as follows:
The code is as follows:
In strcmp (string str1, string str2) // compares uppercase and lowercase letters in a string.
Int strcasecmp (string str1, string str2) // ignore the case-insensitive comparison of letters in the string
The usage of these two functions is similar. you need to input two string parameters for comparison. The input str1 and str2 strings can be compared from the first byte of the Two Strings According to the ASCII value of the byte. if they are equal, the next byte is compared until the comparison is completed. Returns one of the following values:
★If str1 is equal to str2, 0 is returned.
★If str1 is greater than str2, 1 is returned.
★If str1 is smaller than str2,-1 is returned.
In the following program, we can determine the size of two comparative strings by comparing the returned values. Use the strcmp () function to distinguish between uppercase and lowercase letters in a string. use the strcasecmp () function to ignore the comparison of uppercase and lowercase letters in a string. Of course there is no practical significance. The code is as follows:
The code is as follows:
<? Php
$ Username = "Admin ";
$ Password = "lampBrother ";
// Case-insensitive comparison. if two strings are equal, 0 is returned.
If (strcasecmp ($ userName, "admin") = 0 ){
Echo "user name exists ";
}
// Convert the functions corresponding to the two strings into uppercase or lowercase letters, or perform case-insensitive comparison.
If (strcasecmp (strtolower ($ userName), strtolower ("admin") = 0 ){
Echo "user name exists ";
}
// Compare the case sensitivity of letters in a string
Switch (strcmp ($ password, "lampbrother ")){
Case 0:
Echo "two strings are equal
"; Break;
Case 1:
Echo "the first string is greater than the second string
"; Break;
Case-1:
Echo "the first string is smaller than the second string
"; Break;
}
?>
2. string comparison by natural sorting
In addition to the dictionary order of bytes, PHP also provides the "natural sorting" method to compare strings. The so-called natural sorting refers to sorting by People's Daily thinking habits, that is, comparing the numbers in the string by the number size. For example, when comparing byte values, "4" is greater than "33", because "4" is greater than the first character in "33", and "33" is greater than "4" according to the natural sorting rule ". Use the strnatcmp () function to compare two strings by natural sorting. This function is case sensitive and uses a similar format as the strcmp () function.
In the following example, the names of files with numbers in an array are sorted by the bubble sort method in two comparison methods. The code is as follows:
The code is as follows:
<? Php
// Define an array containing numeric values
$ Files = array ("file11.txt", "file22.txt", "file1.txt", "file2.txt ");
Function mySort ($ arr, $ select = false ){
For ($ I = 0; $ I For ($ j; $ j // If the second parameter is true, use the strcmp () function to compare the size.
If ($ select ){
// If the comparison result of the two values is greater than 0, the switch position
If (strcmp ($ arr [$ j], $ arr [j + 1])> 0 ){
$ Tmp = $ arr [$ j];
$ Arr [$ j] = $ arr [$ j + 1];
$ Arr [$ j + 1] = $ tmp;
}
// If the second parameter is false, use the strnatcmp () function to compare the size.
} Else {
// If the comparison result is greater than 0
If (strnatcmp ($ arr [$ j], $ arr [$ j + 1])> 0 ){
$ Tmp = $ arr [$ j];
$ Arr [$ j] = $ arr [$ j + 1];
$ Arr [$ j + 1]; = $ tmp;
}
}
}
}
Return $ arr; // sorted array
}
Print_r (mySort ($ files, true); // Sort by dictionary: file1.txt file11.txt file2.txt file22.txt
Print_r (mySort ($ files, false); // you can select file1.txt file2.txt file11.txt file22.txt in the natural order.
?>
In PHP, the strnatcasecmp () function that ignores case-insensitive versions of this function is also used in the same way as the strnatcmp () function.