How to compare strings in PHP

一个新手
Release: 2023-03-15 19:10:02
Original
2711 people have browsed it

Compare bytes

Comparing strings by bytes is the most commonly used method. The functions that may be used are strcmp() and strcasecmp().

The difference between these two functions is that strcmp() distinguishes the case of characters, while strcasecmp() does not distinguish the case of characters. The usage of the two is basically the same.

Only strcmp() is introduced here;

The syntax is as follows

int strcmp(string str1,string str2)

The parameters str1 and parameter str2 are to be compared. If the two strings are equal, 0 is returned; if the parameter str1 is greater than str2, the return value is greater than 0; if the parameter str1 is less than str2, the return value is less than 0.

For example:

<span style="background-color:rgb(255,228,255); color:rgb(102,0,0)">$str1</span><span style="background-color:rgb(247,250,255)">=</span><span style="background-color:rgb(247,250,255); color:rgb(0,128,0)"><strong>"107</strong></span><span style="background-color:rgb(247,250,255); color:rgb(0,128,0); font-family:宋体"><strong>网站工作室</strong></span><span style="background-color:rgb(247,250,255); color:rgb(0,128,0)"><strong>"</strong></span><span style="background-color:rgb(247,250,255)">;<br></span><span style="background-color:rgb(247,250,255); color:rgb(102,0,0)">$str2</span><span style="background-color:rgb(247,250,255)">=</span><span style="background-color:rgb(247,250,255); color:rgb(0,128,0)"><strong>"107</strong></span><span style="background-color:rgb(247,250,255); color:rgb(0,128,0); font-family:宋体"><strong>网站工作</strong></span><span style="background-color:rgb(247,250,255); color:rgb(0,128,0)"><strong>"</strong></span><span style="background-color:rgb(247,250,255)">;<br></span><span style="background-color:rgb(247,250,255); color:rgb(0,0,128)"><strong>echo </strong></span><span style="background-color:rgb(247,250,255)"><em>strcmp</em></span><span style="background-color:rgb(247,250,255)">(</span><span style="background-color:rgb(228,228,255); color:rgb(102,0,0)">$str1</span><span style="background-color:rgb(247,250,255)">,</span><span style="background-color:rgb(247,250,255); color:rgb(102,0,0)">$str2</span><span style="background-color:rgb(247,250,255)">);<br></span><span style="background-color:rgb(247,250,255); color:rgb(102,0,0)">$str3</span><span style="background-color:rgb(247,250,255)">=</span><span style="background-color:rgb(247,250,255); color:rgb(0,128,0)"><strong>"lab"</strong></span><span style="background-color:rgb(247,250,255)">;<br></span><span style="background-color:rgb(247,250,255); color:rgb(102,0,0)">$str4</span><span style="background-color:rgb(247,250,255)">=</span><span style="background-color:rgb(247,250,255); color:rgb(0,128,0)"><strong>"LAB"</strong></span><span style="background-color:rgb(247,250,255)">;<br></span><span style="background-color:rgb(247,250,255); color:rgb(0,0,128)"><strong>echo </strong></span><span style="background-color:rgb(247,250,255)"><em>strcmp</em></span><span style="background-color:rgb(247,250,255)">(</span><span style="background-color:rgb(247,250,255); color:rgb(102,0,0)">$str3</span><span style="background-color:rgb(247,250,255)">,</span><span style="background-color:rgb(247,250,255); color:rgb(102,0,0)">$str4</span><span style="background-color:rgb(247,250,255)">);<br></span><span style="background-color:rgb(247,250,255); color:rgb(0,0,128)"><strong>echo </strong></span><span style="background-color:rgb(247,250,255)"><em>strcasecmp</em></span><span style="background-color:rgb(247,250,255)">(</span><span style="background-color:rgb(247,250,255); color:rgb(102,0,0)">$str3</span><span style="background-color:rgb(247,250,255)">,</span><span style="background-color:rgb(247,250,255); color:rgb(102,0,0)">$str4</span><span style="background-color:rgb(247,250,255)">);</span>
Copy after login

The running results are as follows:

310

Compare by natural sorting method

In PHP, string comparison according to the natural sorting method is implemented through the strnatcmp() function. The natural sorting method compares the numerical part of the string and sorts the numbers in the string according to size.

The syntax is as follows:

int strnatcmp(string str1,string str2)
Copy after login

The strnatcmp() function uses a "natural" algorithm to compare two strings.

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.

<span style="background-color:rgb(255,228,255); color:rgb(102,0,0)">$str1</span><span style="background-color:rgb(247,250,255)">=</span><span style="background-color:rgb(247,250,255); color:rgb(0,128,0)"><strong>"str3.jpg"</strong></span><span style="background-color:rgb(247,250,255)">;<br/></span><span style="background-color:rgb(247,250,255); color:rgb(102,0,0)">$str2</span><span style="background-color:rgb(247,250,255)">=</span><span style="background-color:rgb(247,250,255); color:rgb(0,128,0)"><strong>"str10.jpg"</strong></span><span style="background-color:rgb(247,250,255)">;<br/></span><span style="background-color:rgb(247,250,255); color:rgb(0,0,128)"><strong>echo </strong></span><span style="background-color:rgb(247,250,255); color:rgb(0,128,0)"><strong>"</strong></span><span style="background-color:rgb(247,250,255); color:rgb(0,128,0); font-family:宋体"><strong>按字节比较:</strong></span><span style="background-color:rgb(247,250,255); color:rgb(0,128,0)"><strong>"</strong></span><span style="background-color:rgb(247,250,255)">.</span><span style="background-color:rgb(247,250,255)"><em>strcmp</em></span><span style="background-color:rgb(247,250,255)">(</span><span style="background-color:rgb(228,228,255); color:rgb(102,0,0)">$str1</span><span style="background-color:rgb(247,250,255)">,</span><span style="background-color:rgb(247,250,255); color:rgb(102,0,0)">$str2</span><span style="background-color:rgb(247,250,255)">).</span><span style="background-color:rgb(247,250,255); color:rgb(0,128,0)"><strong>"<br>"</strong></span><span style="background-color:rgb(247,250,255)">;<br/></span><span style="background-color:rgb(247,250,255); color:rgb(0,0,128)"><strong>echo </strong></span><span style="background-color:rgb(247,250,255); color:rgb(0,128,0)"><strong>"</strong></span><span style="background-color:rgb(247,250,255); color:rgb(0,128,0); font-family:宋体"><strong>按自然排序法比较:</strong></span><span style="background-color:rgb(247,250,255); color:rgb(0,128,0)"><strong>"</strong></span><span style="background-color:rgb(247,250,255)">.</span><span style="background-color:rgb(247,250,255)"><em>strnatcmp</em></span><span style="background-color:rgb(247,250,255)">(</span><span style="background-color:rgb(228,228,255); color:rgb(102,0,0)">$str1</span><span style="background-color:rgb(247,250,255)">,</span><span style="background-color:rgb(247,250,255); color:rgb(102,0,0)">$str2</span><span style="background-color:rgb(247,250,255)">).</span><span style="background-color:rgb(247,250,255); color:rgb(0,128,0)"><strong>"<br>"</strong></span><span style="background-color:rgb(247,250,255)">;<br/></span><span style="background-color:rgb(247,250,255); color:rgb(102,0,0)">$str3</span><span style="background-color:rgb(247,250,255)">=</span><span style="background-color:rgb(247,250,255); color:rgb(0,128,0)"><strong>"mrsoft1"</strong></span><span style="background-color:rgb(247,250,255)">;<br/></span><span style="background-color:rgb(247,250,255); color:rgb(102,0,0)">$str4</span><span style="background-color:rgb(247,250,255)">=</span><span style="background-color:rgb(247,250,255); color:rgb(0,128,0)"><strong>"MRSOFT2"</strong></span><span style="background-color:rgb(247,250,255)">;<br/></span><span style="background-color:rgb(247,250,255); color:rgb(0,0,128)"><strong>echo </strong></span><span style="background-color:rgb(247,250,255); color:rgb(0,128,0)"><strong>"</strong></span><span style="background-color:rgb(247,250,255); color:rgb(0,128,0); font-family:宋体"><strong>按字节比较:</strong></span><span style="background-color:rgb(247,250,255); color:rgb(0,128,0)"><strong>"</strong></span><span style="background-color:rgb(247,250,255)">.</span><span style="background-color:rgb(247,250,255)"><em>strcmp</em></span><span style="background-color:rgb(247,250,255)">(</span><span style="background-color:rgb(247,250,255); color:rgb(102,0,0)">$str3</span><span style="background-color:rgb(247,250,255)">,</span><span style="background-color:rgb(247,250,255); color:rgb(102,0,0)">$str4</span><span style="background-color:rgb(247,250,255)">).</span><span style="background-color:rgb(247,250,255); color:rgb(0,128,0)"><strong>"<br>"</strong></span><span style="background-color:rgb(247,250,255)">;<br/></span><span style="background-color:rgb(247,250,255); color:rgb(0,0,128)"><strong>echo </strong></span><span style="background-color:rgb(247,250,255); color:rgb(0,128,0)"><strong>"</strong></span><span style="background-color:rgb(247,250,255); color:rgb(0,128,0); font-family:宋体"><strong>按自然排序法比较:</strong></span><span style="background-color:rgb(247,250,255); color:rgb(0,128,0)"><strong>"</strong></span><span style="background-color:rgb(247,250,255)">.</span><span style="background-color:rgb(247,250,255)"><em>strnatcmp</em></span><span style="background-color:rgb(247,250,255)">(</span><span style="background-color:rgb(247,250,255); color:rgb(102,0,0)">$str3</span><span style="background-color:rgb(247,250,255)">,</span><span style="background-color:rgb(247,250,255); color:rgb(102,0,0)">$str4</span><span style="background-color:rgb(247,250,255)">).</span><span style="background-color:rgb(247,250,255); color:rgb(0,128,0)"><strong>"<br>"</strong></span><span style="background-color:rgb(247,250,255)">;</span>
Copy after login

The output result is:

Compare by bytes: 1
Compare by natural sorting: -1
Compare by bytes: 1
By natural sorting Comparison: 1

$str1 = "mrsoft1";
$str2 = "MRSOFT2";
echo strnatcmp($str1,$str2); //1 因为 m 大于 M
$str1 = "mrsoft1";
$str2 = "mrsoft2";
echo strnatcmp($str1,$str2); //-1 这样才对
$str1 = "mrsoft11";
$str2 = "mrsoft2";
echo strnatcmp($str1,$str2); //1 这才是本意 11 大于 2
Copy after login

Description: When comparing according to natural order, there is another function that has the same function as strnatcmp(), but is not case-sensitivestrnatcasecmp()

<span style="font-size: 20px; color: rgb(255, 0, 0);">指定从源字符串的位置比较</span><br/>
Copy after login
<pre style="font-family:Consolas; font-size:12pt; background-color:rgb(255,255,255)">strncmp()函数用来比较字符串中的前n个字符,该函数区分大小写
Copy after login
语法如下:
Copy after login
int strncmp(string str1,string str2,int len)
Copy after login
参数str1规定要比较的首个字符串。参数str2规定要比较的第二个字符串。len(必需)规定比较中所用的每个字符串的字符数。
Copy after login
<span style="font-family:微软雅黑; font-size:14px">如果相等则返回0;如果参数str1大于str2则返回值大于0;</span><span style="font-family:微软雅黑; font-size:14px">如果参数str1小于str2则返回值小于0。</span><br/>
Copy after login
例如:
Copy after login
运算结果为
Copy after login
-1
Copy after login

Tips: This function is similar to the strcmp() function, the difference is that strcmp() No length parameter

The above is the detailed content of How 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