Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Is there a string comparison method in php?

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "there is no string comparison method in php", the content is simple and clear, and I hope it can help you solve your doubts. Let me lead you to study and learn this article "is there any string comparison method in php?"

There is a string comparison method in php. Php has a variety of built-in string comparison methods: strcasecmp (), strcmp (), strcoll (), strnatcasecmp (), strnatcmp (), strncasecmp (), strncmp (), and so on.

Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

There is a string comparison method in php.

Php has several built-in string comparison methods:

The function description strcasecmp () compares two strings (case-insensitive). Strcmp () compares two strings (case sensitive). Strcoll () compares two strings (based on local settings). Strnatcasecmp () uses a "natural sort" algorithm to compare two strings (case-insensitive). Strnatcmp () uses a "natural sort" algorithm to compare two strings (case-sensitive). String comparison of the first n characters of strncasecmp () (case insensitive). String comparison of the first n characters of strncmp () (case sensitive). Substr_compare () compares two strings from the specified starting position (binary security and selective case sensitivity).

1. Compare strings by byte

There are two ways to compare strings by bytes, using the strcmp () function and the strcasecmp () function. The difference between the two functions is that the strcmp () function is case-sensitive while the strcasecmp () function is not case-sensitive. Since the implementation of these two functions is basically the same, I just take out the strcmp () function to introduce it.

The strcmp () function is used to compare two strings by bytes.

The syntax format is as follows:

Strcmp (string1, string2)

Note that this function is case sensitive. The parameter string1 and the parameter string2 specify two strings to compare. If equal, the return value of the function is 0; if the parameter string1 is greater than the parameter string2, the return value of the function is greater than 0; if the parameter string1 is less than the parameter string2, the return value of the function is less than 0.

Use the srtcmp () function and the strcasecmp () function to compare two strings by bytes, respectively. The code example is as follows:

The output is as follows:

0 1 0

Description: in PHP, the application of comparing strings is very widespread. For example, use the strcmp () function to compare whether the user name and password entered in the user login system are correct, and if you do not use this function when authenticating the user and password, you can log in as long as you enter the user name and password, whether in uppercase or lowercase. After using the srtcmp () function, this situation is avoided, it is correct in time, and you must match all case before you can log in, thus improving the security of the website.

two。 Compare strings according to natural sorting method

In PHP, comparing strings according to natural sorting is achieved by using the strnatcmp () function. The admission sorting method compares the numeric part of a string, comparing the numbers in the string by size. Its syntax is in the following format:

Strnatcmp (string1, string2)

Tip: this function is case sensitive. If the parameter string1 and parameter string2 are equal, the return value of the function is 0; if the parameter string1 is greater than the parameter string2, the function return value is greater than 0; if the parameter string1 is less than the parameter string2, the function return value is less than 0.

Note: in the natural algorithm, 2 is smaller than 10, while in the computer sequence, 10 is smaller than 2, because the first number in "10" is "1", which is less than "2".

The example code for comparing strings according to natural sorting using the strnatcmp () function is as follows:

The result of the output is:

-1 1

Note: according to the admission sorting method for comparison, you can also use another strnatcmp () function, which has the same effect as the strnatcasecmp () function, but does not distinguish between sizes.

3. Specifies a function to compare from the position of the source string

The strncmp () function is used to compare the first n characters in a string.

Its syntax is in the following format:

Strncmp (string1, string2, length)

Its parameters are described as follows: if parameter string1 and parameter string2 are equal, the return value of function is 0; if parameter string1 is greater than parameter string2, the return value of function is greater than 0; if parameter string1 is less than parameter string2, the return value of function is less than 0. This function is case sensitive.

The parameter states that string1 specifies the first string object participating in the comparison string2 specifies the necessary parameters of the second string object length participating in the comparison, and specifies the number of strings participating in the comparison in each string

Use the strncmp () function to compare whether the first two characters of a string are equal to the source string. In fact, the example code is as follows:

The output is as follows:

-1

Note: as you can see from the above code, because the first letter of the string in the variable $str2 is lowercase and does not match the string in the variable $str1, the function returns a value of-1 after comparing the two strings.

The above is all the content of the article "is there any string comparison method in php"? thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report