Home > Backend Development > PHP Problem > What is the function to compare strings in php

What is the function to compare strings in php

青灯夜游
Release: 2023-03-16 19:14:01
Original
2266 people have browsed it

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.

What is the function to compare strings in php

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>";
?>
Copy after login

Output:

What is the function to compare strings in php

The strcmp() function is case-sensitive when comparing strings.

<?php
$str1="PHP中文网";
$str2="php中文网";
echo strcmp($str1,$str2)."<br>";
?>
Copy after login

Output result:

-1
Copy after login

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 = &#39;php124@qq&#39;;
$pwd2 = &#39;php124@QQ&#39;;
if(strcmp($pwd1, $pwd2) != 0){
    echo &#39;密码不匹配!&#39;;
} else {
    echo &#39;密码匹配!&#39;;
}
?>
Copy after login

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.

What is the function to compare strings in php

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 &#39;两个网址相同&#39;;
    } else {
        echo &#39;两个网址不同&#39;;
    }
?>
Copy after login

Output result:

两个网址相同
Copy after login

3. strncasecmp() function--case insensitive

strncasecmp() function compares two strings (not case sensitive). The syntax is as follows

strncasecmp(string1,string2,length)
Copy after login
##ParameterDescriptionRequired. 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.

返回值:

  • 0 - 如果两个字符串相等

  • <0 - 如果 string1 小于 string2

  • >0 - 如果 string1 大于 string2

示例:

<?php
echo strncasecmp("Hello world!","hello earth!",6);
?>
Copy after login

输出:

0
Copy after login

4、strnatcasecmp()函数--大小写不敏感

strnatcasecmp() 函数使用一种"自然"算法来比较两个字符串(不区分大小写)。

在自然算法中,数字 2 小于数字 10。在计算机排序中,10 小于 2,这是因为 10 中的第一个数字小于 2。

语法:

strnatcasecmp(string1,string2)
Copy after login

返回值:

  • 0 - 如果两个字符串相等

  • <0 - 如果 string1 小于 string2

  • >0 - 如果 string1 大于 string2

示例:

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

输出:

-1
1
Copy after login

5、strncmp()函数--大小写敏感

strncmp() 函数比较两个字符串(区分大小写)。

该函数与 strcmp() 函数类似,不同的是,strcmp() 没有 length 参数(不能指定每个字符串用于比较的字符数)。

strncmp(string1,string2,length)
Copy after login
string1
string2
length
参数描述
string1必需。规定要比较的第一个字符串。
string2必需。规定要比较的第二个字符串。
length必需。规定每个字符串用于比较的字符数。

返回值和strcmp() 函数一样

<?php
header("Content-type:text/html;charset=utf-8");
$pwd1 = &#39;password&#39;;
$pwd2 = &#39;Password&#39;;
if(strncmp($pwd1, $pwd2,8) != 0){
    echo &#39;密码不匹配!&#39;;
} else {
    echo &#39;密码匹配!&#39;;
}
?>
Copy after login

What is the function to compare strings in php

推荐学习:《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!

Related labels:
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