Summary of the use of PHP string comparison functions strcmp of and strcasecmp of

  • 2021-08-05 09:11:08
  • OfStack

Comparing strings is one of the important features in the string processing function of any programming language. In PHP, in addition to using comparison operation symbols (= =, < Or > ), a series of comparison functions are provided to enable PHP to perform more complex string comparisons, such as strcmp (), strcasecmp (), and strnatcmp ().

1. Compare strings in byte order

To compare strings in byte order, use functions strcmp () and strcasecmp (), where function strcasecmp () ignores the case of the letters in the string for comparison. The prototype of these two functions is as follows:


in strcmp(string str1,string str2)               // Case-sensitive comparison of letters in strings
int strcasecmp(string str1,string str2)                // Ignore case comparison of letters in strings

The usage of these two functions is similar, and both of them need to pass in two string parameters for comparison. You can compare the input str1 and str2 strings from the first byte of the two strings according to the ASCII value of bytes, and if they are equal, enter the comparison of the next byte until the end of the comparison. Returns one of the following 3 values:
★ Returns 0 if str1 equals str2.
★ Returns 1 if str1 is greater than str2.
★ Returns-1 if str1 is less than str2.
In the following program, the size of two comparison strings is judged by the return value after comparison. Use the strcmp () function to distinguish case comparisons of letters in strings, and use the strcasecmp () function to ignore case comparisons of letters in strings. Of course, it has no practical significance. The code looks like this:

<?php
$username = "Admin";
$password = "lampBrother";
 
// Case-insensitive comparison, returned if two strings are equal 0
if(strcasecmp($userName,"admin")== 0){
echo " User name exists ";
}
// You can also achieve case-insensitive comparisons by converting the corresponding functions of two compared strings to all uppercase or all lowercase
if(strcasecmp(strtolower($userName),strtolower("admin")) == 0){
echo " User name exists ";
}
 
// Case comparison of letters in strings
switch(strcmp($password,"lampbrother")){
case 0:
echo " Two strings are equal <br>"; break;
case 1:
echo " No. 1 1 A string is greater than the first 2 String <br>"; break;
case -1:
echo " No. 1 1 A string is less than the first 2 String <br>"; break;
}
?>

2. Compare strings in natural sort

In addition to comparing strings in dictionary order of byte bits, PHP also provides a "natural sort" method for comparing strings. The so-called natural sorting refers to sorting according to people's thinking habits in daily life, that is, comparing the number parts in the string according to the number size. For example, "4" is greater than "33" in byte comparison, because "4" is greater than the first character in "33", while "33" is greater than "4" in natural sorting. Compare two strings in a natural sort using the strnatcmp () function, which is case sensitive and uses a format similar to the strcmp () function.

In the following example, file names with numbers in an array are sorted by two comparison methods using bubble sorting. The code looks like this:


<?php
// Definition 1 Arrays containing numeric values
$files = array("file11.txt","file22.txt","file1.txt","file2.txt");
 
function mySort($arr,$select = false){
    for($i=0;$i<count($arr);$i++){
        for($j;$j<count($arr)-1;$j++){
        // If the 2 Parameters are ture Use the strcmp() Function comparison size
            if($select){
            // The comparison result of the two values before and after is greater than 0 Switch positions
                if(strcmp($arr[$j],$arr[j+1])>0){
                    $tmp = $arr[$j];
                    $arr[$j] = $arr[$j+1];
                    $arr[$j+1] = $tmp;
                }
           // If the 2 Parameters are false Use the strnatcmp() Function comparison size
           }else{
            // If the comparison result is greater than 0 Switch position
                if(strnatcmp($arr[$j],$arr[$j+1])>0){
                    $tmp = $arr[$j];
                    $arr[$j] = $arr[$j+1];
                    $arr[$j+1]; = $tmp;
                }
            }
        }
    }
return $arr; // Array after sorting
}
print_r(mySort($files,true));         // Select Sort in Dictionary Order: file1.txt file11.txt file2.txt file22.txt
print_r(mySort($files,false));          // Select Sort in Natural Order: file1.txt file2.txt file11.txt file22.txt
?>

A case-ignoring version of this function is also provided in PHP. The strnatcasecmp () is used the same way as the strnatcmp () function.


Related articles: