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:
"; $str1="12"; $str2="13"; echo strcmp($str1,$str2)."
"; $str1="15"; $str2="13"; echo strcmp($str1,$str2)."
"; ?>
Output:
The strcmp() function is case-sensitive when comparing strings.
"; ?>
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.
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:
Output result:
两个网址相同
3. strncasecmp() function--case insensitive
strncasecmp() function compares two strings (not case sensitive). The syntax is as follows
strncasecmp(string1,string2,length)
返回值:
0 - 如果两个字符串相等
<0 - 如果 string1 小于 string2
>0 - 如果 string1 大于 string2
示例:
输出:
0
4、strnatcasecmp()函数--大小写不敏感
strnatcasecmp() 函数使用一种"自然"算法来比较两个字符串(不区分大小写)。
在自然算法中,数字 2 小于数字 10。在计算机排序中,10 小于 2,这是因为 10 中的第一个数字小于 2。
语法:
strnatcasecmp(string1,string2)
返回值:
0 - 如果两个字符串相等
<0 - 如果 string1 小于 string2
>0 - 如果 string1 大于 string2
示例:
"; echo strnatcasecmp("10Hello world!","2Hello WORLD!"); ?>
输出:
-1 1
5、strncmp()函数--大小写敏感
strncmp() 函数比较两个字符串(区分大小写)。
该函数与 strcmp() 函数类似,不同的是,strcmp() 没有 length 参数(不能指定每个字符串用于比较的字符数)。
strncmp(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视频教程》
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!