PHP string comparison functions strcmp () and strcasecmp () using summary _php Tips

Source: Internet
Author: User
Tags first string lowercase strcmp

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

1. String comparisons in byte order

To compare strings in byte order, you can use the 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 Code code as follows:

In strcmp (string str1,string str2)//distinguish between uppercase and lowercase letters in strings
int strcasecmp (string str1,string str2)//ignoring letter capitalization comparisons in strings

The two functions are similar in usage, requiring two string parameters to be passed in for comparison. You can compare the str1 and str2 two strings entered, starting with the byte's ASCII value from the first byte of the two strings, and if the equality goes to the next byte comparison until the end of the comparison. Returns one of the following three values:
★ Return 0 if the str1 equals str2.
★ Return 1 if STR1 is greater than str2.
★ Return-1 if str1 is less than str2.
The two comparison string sizes are judged by the return value after comparison in the following program. Use the strcmp () function to distinguish between uppercase and lowercase comparisons in strings, and use the strcasecmp () function to ignore the case of letters in a string. Of course there is no practical significance. The code looks like this:
Copy Code code as follows:

<?php
$username = "Admin";
$password = "Lampbrother";

Case-insensitive comparisons, if two strings equal returns 0
if (strcasecmp ($userName, "admin") = = 0) {
echo "username exists";
}
You can also implement a case-insensitive comparison when the corresponding function of two comparison strings is turned to uppercase or lowercase
if (strcasecmp (Strtolower ($userName), Strtolower ("admin")) = = 0) {
echo "username exists";
}

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

2. String comparisons by natural ordering

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

In the following example, the name of a file with a number in an array is sorted by two comparison methods using the bubble sort method. The code looks like this:

Copy Code code as follows:

<?php
Defines an array that contains 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 second argument is ture, the strcmp () function is used to compare the size
if ($select) {
Two values compare results greater than 0 swap positions
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 locations
if (strnatcmp ($arr [$j], $arr [$j +1]) >0) {
$tmp = $arr [$j];
$arr [$j] = $arr [$j +1];
$arr [$j +1]; = $tmp;
}
}
}
}
return $arr; Array after sort
}
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
?>

This function is also provided in PHP that ignores the case version of the function strnatcasecmp () is the same as the strnatcmp () function.

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.