How to perform case-insensitive comparison of strings in php
Method: 1. Use the "strcasecmp(string1,string2)" statement; 2. Use the "strncasecmp(string1,string2,length)" statement; 3. Use the "strnatcasecmp(string1,string2)" statement.
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)
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 ; if $str1 is greater than $str2, the return value is <code>> 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 = 'https://www.php.cn/'; $url2 = 'HTTPS://www.php.cn/'; if(strcasecmp($url1, $url2) == 0){ echo '两个网址相同'; } else { echo '两个网址不同'; } ?>
Output:
两个网址相同
Method 2: Use the strncasecmp() function
The strncasecmp() function compares two strings (not case-sensitive).
Syntax
strncasecmp(string1,string2,length)
Parameters | Description |
---|---|
string1 | Required. Specifies the first string to compare. |
string2 | Required. Specifies the second string to be compared. |
length | Required. Specifies the number of characters per string used for comparison. |
Return value: This function returns:
0
- If two strings Equality- if string1 is less than string2
##>0
- if string1 is greater than string2
<?php echo strncasecmp("Hello","Hello",6); echo "<br>"; echo strncasecmp("Hello","hELLo",6); ?>Output:
0 0
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. Syntaxstrnatcasecmp(string1,string2)
Description | |
---|---|
string1 | Required. Specifies the first string to compare.|
string2 | Required. Specifies the second string to be compared.
-
0
- If the two strings are equal
- 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!"); ?>
Output:
-1 1
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);
?>
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 )
Recommended learning: "
PHP Video TutorialThe 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!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Zend Studio 13.0.1
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools