php functions for comparing strings: 1. strcasecmp(), case-insensitive comparison of two strings; 2. strcmp(), case-sensitive comparison of two strings; 3. strcoll( ), compares two strings according to local settings; 4. strnatcasecmp(), which is case-insensitive and uses a "natural sorting" algorithm to compare two strings; 5. strnatcmp(), which is case-sensitive and uses a "natural sorting" algorithm to compare two strings; A "natural ordering" algorithm to compare two strings.
The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer
php has a variety of built-in strings Comparison function
Function | Description |
---|---|
strcasecmp() | Compare Two strings (case insensitive). |
strcmp() | Compares two strings (case sensitive). |
strcoll() | Compares two strings (according to local settings). |
strnatcasecmp() | Uses a "natural ordering" algorithm to compare two strings (case insensitive). |
strnatcmp() | Uses a "natural ordering" algorithm to compare two strings (case sensitive). |
strncasecmp() | String comparison of the first n characters (case insensitive). |
strncmp() | String comparison of the first n characters (case sensitive). |
substr_compare() | Compares two strings starting at the specified starting position (binary safe and selective case sensitive). |
Commonly used string comparison functions
1. strcmp() function--with Compare two strings in a case-sensitive manner
strcmp(string1,string2)
When comparing two strings, if:
If the two strings are equal, 0
will be returned;
If string1 is less than string2, will be returned Value;
If string1 is greater than string2, the value of > 0
will be returned;
Let’s look at it first An example below:
<?php $str1="12"; $str2="12"; echo strcmp($str1,$str2)."<br>"; $str1="12"; $str2="13"; echo strcmp($str1,$str2)."<br>"; $str1="15"; $str2="13"; echo strcmp($str1,$str2)."<br>"; ?>
Output:
The strcmp() function is case-sensitive when comparing strings.
<?php $str1="PHP中文网"; $str2="php中文网"; echo strcmp($str1,$str2)."<br>"; ?>
Output result:
-1
With the help of this feature, we can use it in the function of confirming user password when registering an account on the website (passwords are usually case-sensitive), Compare the passwords entered twice to see if they are consistent.
<?php header("Content-type:text/html;charset=utf-8"); $pwd1 = 'php124@qq'; $pwd2 = 'php124@QQ'; if(strcmp($pwd1, $pwd2) != 0){ echo '密码不匹配!'; } else { echo '密码匹配!'; } ?>
Only if two strings completely match, the strcmp() function will consider them equal; in the above example, $pwd1 and $pwd2 have different case, so the two strings are not the same. equal.
But sometimes, we only need to compare the values of strings and do not want to be case sensitive, such as comparing website URLs, so how to deal with it? Simple, you can use strcasecmp() function.
2. strcasecmp() function - compares two strings in a case-insensitive manner
The strcasecmp() function is similar to the strcmp() function. Comparable strings, the return value is also the same, the only difference is that the strcasecmp() function is not case sensitive.
Let’s take a look at the following example:
<?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 result:
两个网址相同
3. strncasecmp() function--case insensitive
strncasecmp() function compares two strings (not case sensitive). The syntax is as follows
strncasecmp(string1,string2,length)
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.
参数 | 描述 |
---|---|
string1 | 必需。规定要比较的第一个字符串。 |
string2 | 必需。规定要比较的第二个字符串。 |
length | 必需。规定每个字符串用于比较的字符数。 |
返回值和strcmp() 函数一样
<?php header("Content-type:text/html;charset=utf-8"); $pwd1 = 'password'; $pwd2 = 'Password'; if(strncmp($pwd1, $pwd2,8) != 0){ echo '密码不匹配!'; } else { echo '密码匹配!'; } ?>
推荐学习:《PHP视频教程》
The above is the detailed content of What is the function to compare strings in php. For more information, please follow other related articles on the PHP Chinese website!