PHP string comparison functions strcmp () and strcasecmp () use summary, strcmpstrcasecmp_php Tutorial

Source: Internet
Author: User
Tags first string strcmp

PHP string comparison functions strcmp () and strcasecmp () use summary, strcmpstrcasecmp


Comparing strings is one of the most important features of string handling in any programming language. In addition to using the comparison operation notation (= =, <或> ) in PHP, a series of comparison functions are provided, allowing PHP to perform more complex string comparisons. Functions such as strcmp (), strcasecmp (), and strnatcmp ().

1. String comparisons by byte order

To compare strings in byte order, you can use strcmp () and strcasecmp () two functions, where the function strcasecmp () can be compared by ignoring the case of the letters in the string. The prototypes for these two functions are as follows:
Copy CodeThe code is as follows:
In strcmp (string str1,string str2)//Distinguish the letter case comparison in a string
int strcasecmp (string str1,string str2)//Ignore letter case comparison in string

These two functions are similar in usage and require passing in two string arguments for comparison. You can compare the input str1 and str2 two strings by the ASCII value of the bytes from the first byte of the two strings, or the next byte if they are equal, up to the end of the comparison. Returns one of the following three values:
★ returns 0 if STR1 equals str2.
★ Returns 1 if STR1 is greater than str2.
★ Return-1 if str1 is less than str2.
In the following program, two comparison string sizes are determined by comparing the return values. Use the strcmp () function to differentiate between case-by-case comparisons in strings, and use the strcasecmp () function to ignore the case-sensitive comparisons of letters in strings. Of course there is no practical meaning. The code looks like this:
Copy CodeThe code is as follows:
<?php
$username = "Admin";
$password = "Lampbrother";

Case-insensitive comparison if two strings are returned equal to 0
if (strcasecmp ($userName, "admin") = = 0) {
echo "User name exists";
}
A case-insensitive comparison can also be achieved when the corresponding function of the two comparison string is turned to uppercase or full lowercase.
if (strcasecmp (Strtolower ($userName), Strtolower ("admin")) = = 0) {
echo "User name exists";
}

Distinguish the case comparison of letters in a string
Switch (strcmp ($password, "Lampbrother")) {
Case 0:
echo "Two strings equal
"; Break
Case 1:
echo "The first string is greater than the second string
"; Break
Case-1:
echo "The first string is less than the second string
"; Break
}
?>

2. String comparisons by natural sorting

In addition to being able to compare the dictionary order of byte bits, PHP provides a "natural sort" method for comparing strings. The so-called natural ordering, refers to according to People's daily life in the habit of thinking to sort, will be the number of the string in accordance with the numerical size comparison. For example, "4" is greater than "33" when compared by Byte, because "4" is greater than the first character in "33", and according to the natural ordering rule "33" is greater than "4". Use the strnatcmp () function to compare two strings in natural order, which is case-sensitive, and uses a format similar to the strcmp () function.

In the following example, a file name with numbers in an array is sorted by using the bubbling sort method with two comparison methods. The code is as follows: The
copy Code 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 ture, use the strcmp () function to compare the size of
if ($select) {
///two values before and after the comparison result is greater than 0 if the interchange position is
if (strcmp ($arr [$j], $arr [j+1]) >0) {
$tmp = $arr [$j];
$arr [$j] = $arr [$j +1];
$arr [$j +1] = $tmp;
}
//If the second argument is false, use the STRNATCMP () function to compare the size
}else{
//If the comparison result is greater than 0 swap position
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));//select Sort by dictionary order: file1.txt File11.txt file2.txt file22.txt
Print_r (Mysort ($files, false));//select Sort by natural order: File1.txt File2.txt File11.txt File22.txt
?

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

http://www.bkjia.com/PHPjc/914064.html www.bkjia.com true http://www.bkjia.com/PHPjc/914064.html techarticle The php string comparison function strcmp () and strcasecmp () use a summary, strcmpstrcasecmp comparison string is one of the most important features of any programming language's string processing capabilities. In addition to PHP ...

  • 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.