PHP method of comparing strings without case sensitivity: 1. Use the "strcasecmp(String 1, String 2)" statement; 2. Use the "strnatcasecmp (String 1, String 2)" statement ;3. Use the "strncasecmp(String 1, String 2, length)" statement.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
Method 1: Use strcasecmp()--Case insensitive
strcasecmp() function compares two strings. The syntax is as follows
strcasecmp(string1,string2)
Description | |
---|---|
string1 | Required. Specifies the first string to compare.|
string2 | Required. Specifies the second string to be compared.
Example:
<?php echo strcasecmp("Hello world!","HELLO WORLD!"); ?>
0
Method 2: Use strnatcasecmp() function--case insensitive
strnatcasecmp The () 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)
Example:
<?php echo strnatcasecmp("2Hello world!","10Hello WORLD!"); echo "<br>"; echo strnatcasecmp("10Hello world!","2Hello WORLD!"); ?>
-1 1
Method 2: Use strncasecmp() function--case insensitive
strncasecmp() function compares two strings (case-insensitive). The syntax is as followsstrncasecmp(string1,string2,length)
Description | |
---|---|
Required. Specifies the first string to compare. | |
Required. Specifies the second string to be compared. | |
Required. Specifies the number of characters per string used for comparison. |
<?php echo strncasecmp("Hello world!","hello earth!",6); ?>
0
Recommended learning: "
PHP Video Tutorial
The above is the detailed content of How to compare strings in php (case insensitive). For more information, please follow other related articles on the PHP Chinese website!