Home > Backend Development > PHP Problem > How to perform case-insensitive comparison of strings in php

How to perform case-insensitive comparison of strings in php

青灯夜游
Release: 2023-03-11 17:34:02
Original
2067 people have browsed it

Method: 1. Use the "strcasecmp(string1,string2)" statement; 2. Use the "strncasecmp(string1,string2,length)" statement; 3. Use the "strnatcasecmp(string1,string2)" statement.

How to perform case-insensitive comparison of strings in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

Method 1: Use strcasecmp() Function

strcasecmp() function is not case-sensitive when comparing strings. Its syntax format is as follows:

strcasecmp(string $str1, string $str2)
Copy after login

Among them, $str1 and $str2 are the two characters to be compared. string.

According to the comparison result, if $str1 is less than $str2, the return value < 0; if $str1 is greater than $str2, the return value is > 0; If $str1 is equal to $str2, 0 is returned.

Website URLs are not case-sensitive, so we can use the strcasecmp() function to compare whether two URLs are the same. The sample code is as follows:

<?php
header("Content-type:text/html;charset=utf-8");
$url1 = &#39;https://www.php.cn/&#39;;
$url2 = &#39;HTTPS://www.php.cn/&#39;;
if(strcasecmp($url1, $url2) == 0){
    echo &#39;两个网址相同&#39;;
} else {
    echo &#39;两个网址不同&#39;;
}
?>
Copy after login

Output:

两个网址相同
Copy after login

Method 2: Use the strncasecmp() function

The strncasecmp() function compares two strings (not case-sensitive).

Syntax

strncasecmp(string1,string2,length)
Copy after login
ParametersDescription
string1 Required. Specifies the first string to compare.
string2Required. Specifies the second string to be compared.
lengthRequired. Specifies the number of characters per string used for comparison.

Return value: This function returns:

  • 0 - If two strings Equality

  • <0 - if string1 is less than string2

  • ##>0 - if string1 is greater than string2

Example:


<?php
echo strncasecmp("Hello","Hello",6);
echo "<br>";
echo strncasecmp("Hello","hELLo",6);
?>
Copy after login

Output:

0
0
Copy after login

Method 3: Use strnatcasecmp() function

The strnatcasecmp() function uses a "natural" algorithm to compare two strings (case-insensitive).

In natural algorithms, the number 2 is less than the number 10. In computer sorting, 10 is less than 2 because the first number in 10 is less than 2.

Syntax

strnatcasecmp(string1,string2)
Copy after login

ParametersDescriptionRequired. Specifies the first string to compare. Required. Specifies the second string to be compared.
string1
string2
Return value:

  • 0 - If the two strings are equal

  • <0 - if string1 is less than string2

  • ##>0

    - if string1 is greater than string2

  • Example:
<?php
echo strnatcasecmp("2Hello world!","10Hello WORLD!");
echo "<br>";
echo strnatcasecmp("10Hello world!","2Hello WORLD!");
?>
Copy after login

Output:

-1
1
Copy after login

Description:

Natural algorithm (strnatcmp) and conventional computer string sorting algorithm ( strcmp)

<?php
$arr1 = $arr2 = array("pic1","pic2","pic10","pic01","pic100","pic20","pic30","pic200");
echo "Standard string comparison"."<br>";
usort($arr1,"strcmp");

print_r($arr1);
echo "<br>";
echo "<br>";

echo "Natural order string comparison"."<br>";
usort($arr2,"strnatcmp");

print_r($arr2);
?>
Copy after login
Output:

Standard string comparison
Array ( [0] => pic01 [1] => pic1 [2] => pic10 [3] => pic100 [4] => pic2 [5] => pic20 [6] => pic200 [7] => pic30 )

Natural order string comparison
Array ( [0] => pic01 [1] => pic1 [2] => pic2 [3] => pic10 [4] => pic20 [5] => pic30 [6] => pic100 [7] => pic200 )
Copy after login

Recommended learning: "

PHP Video Tutorial

"

The above is the detailed content of How to perform case-insensitive comparison of strings in php. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template