PHP string comparison functions

Source: Internet
Author: User
In PHP, there are many methods to compare strings. The first is to use the srtcmp () function and the strcasecmp () function to compare bytes, and the second is to use strnatcmp () functions are compared according to the natural sorting method. The third is to use the strncmp () function to specify the start position of the source string for comparison. In PHP, there are many methods for comparing strings. The first method is to use the strcmp () function and the strcasecmp () function to compare bytes, and the second method is to use strnatcmp () functions are compared according to the natural sorting method. The third is to use the strncmp () function to specify the start position of the source string for comparison. The following describes these methods in depth.

1. compare strings by byte

There are two methods for comparing strings by byte: using the strcmp () function and strcasecmp () function. The difference between the two functions is that the strcmp () function is case sensitive, while the strcasecmp () function is case insensitive. Since the implementation methods of these two functions are basically the same, here we will introduce the strcmp () function.

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

The syntax format is as follows:

 strcmp(string1, string2)

The string1 and string2 parameters specify the two strings to be compared. If the value is equal, the return value is 0. if the value of string1 is greater than the value of string2, the return value is greater than 0. if the value of string1 is smaller than the value of string2, the return value is less than 0.

Note that this function is case sensitive.

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

 

Output result:

0 1 0

Note: In PHP, it is widely used to compare strings. For example, use the strcmp () function to compare whether the user name and password entered in the user logon system are correct. if this function is not used to verify the user and password, enter the username and password in both upper and lower case. This situation is avoided after the srtcmp () function is used. it is correct in a timely manner and must be case-insensitive. this improves the security of the website.

2. compare strings by natural sorting

In PHP, string comparison by natural sorting is achieved by using the strnatcmp () function. The self-check sorting method compares the numbers in a string by size. Its syntax is as follows:

strnatcmp(string1, string2)

If string1 is equal to string2, the return value is 0. if string1 is greater than string2, the return value is greater than 0. if string1 is less than string2, the return value is less than 0.

Tip: This function is case sensitive.

Note: in the natural algorithm, 2 is smaller than 10, but 10 is smaller than 2 in the computer sequence, because the first digit in "10" is "1 ", it is smaller than "2.

The instance code for comparing strings using the strnatcmp () function by natural sorting method is as follows:

 

The output result is:

-1  1

Note: For comparison based on the self-recognition sorting method, another strnatcasecmp () function can also be used for the same purpose as the strnatcmp () function, but does not differentiate the size.

3. specify the function for comparison from the source string position

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

Its syntax is as follows:

strncmp(string1, string2, length)

If string1 is equal to string2, the return value is 0. if string1 is greater than string2, the return value is greater than 0. if string1 is less than string2, the return value is less than 0. This function is case sensitive.

Its parameters are described as follows:

Parameter Description
String1 Specifies the first string object involved in the comparison
String2 Specify the second string object involved in the comparison
Length Required parameter to specify the number of strings involved in the comparison

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

 

Output result:

 -1

Note: As shown in the code above, because the first letter of the string in variable $ str2 is lower-case, it does not match the string in variable $ str1, therefore, the return value of the function after two strings are compared is-1.

The above is a detailed description of the PHP string comparison function. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.