Home > Backend Development > PHP Problem > PHP uses one trick to compare the similarities and differences of strings using the 'natural' algorithm (case sensitive)

PHP uses one trick to compare the similarities and differences of strings using the 'natural' algorithm (case sensitive)

醉折花枝作酒筹
Release: 2023-03-11 22:06:01
Original
2277 people have browsed it

In the previous article, we learned about the method of using the "natural" algorithm to know the similarities and differences of strings. If you need it, please read "How to use the "natural" algorithm to know the similarities and differences of strings". This time we introduce to you another method of using the "natural" algorithm to know the similarities and differences of strings. You can refer to it if you need it.

In the previous article, we introduced a "natural" algorithm that is case-insensitive. But, friends, do you have any doubts? When using it, if you need to ensure that the letters in a string are in the same case, how should you write it? Today I will tell you how a case-sensitive "natural" algorithm compares strings.

Let us look at a little chestnut.

<?php
echo strnatcmp("2Hello WORLD!","10Hello WORLD!");
echo "<br>";
echo strnatcmp("2hello world!","2aello world!");
echo "<br>";
echo strnatcmp("2hello world!","9hello WORLD!");
echo "<br>";
echo strnatcmp("2hello world!","2HELLO WORLD!");
echo "<br>";
echo strnatcmp("2hello world!","2hello world!");
?>
Copy after login

The result is

PHP uses one trick to compare the similarities and differences of strings using the natural algorithm (case sensitive)

Let’s take a look at this result. The result of this function still uses “0”, “ -1", "1" to represent the result of comparison between two functions. Let’s look at this example

When we have the same number except for the previous number, we find that the number is -1, which means that the first string is smaller than the second string; when our numbers are the same, but the following When the letters change, it is 1, which means that the first string is greater than the second string; and when the numbers are the same, the letters are the same, and the upper and lower case are different, it is found to be 1; only when the numbers, letters, And when the uppercase and lowercase letters are exactly the same, it is 0, that is, the two strings are equal.

Then let’s take a look at this function.

The strnatcmp() function uses a "natural" algorithm to compare two strings (case sensitive). In natural arithmetic, 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.

The syntax of this function is

strnatcmp(要比较的第一个字符串,要比较的第二个字符串)
Copy after login

These are the same as the functions we introduced in the previous article, but we still have to look at the return value of this function.

This function returns:

  • 0 - if the two strings are equal

  • >0 - If string1 is greater than string2

That’s it. If you want to know anything else, you can click this. → →php video tutorial

The above is the detailed content of PHP uses one trick to compare the similarities and differences of strings using the 'natural' algorithm (case sensitive). For more information, please follow other related articles on the PHP Chinese website!

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