Tips for comparing PHP strncasecmp strings

Source: Internet
Author: User

In this case, the conversion is required twice. Most of the time, we are for character set conversion will be like this, such as determining whether the parameter passed in UTF-8, the five character writing, but more, such as UTF-8, Utf-8, what should we do with UTF-8? Strtolower? Strupper? No ..
Strncasecmp ($ A, $ B, $ length ..
If the return value is 0, it is equal. How can we determine?
Strncasecmp ($ STR, 'utf-8', 5) = 0. Then, the input parameter is utf8. Is it convenient?
These functions are not commonly used. I can see that the function is used in yii framework. When processing events and determining whether the first two characters are on, this is the case. I also learned a trick.

Strncasecmp definition and usage
Definition and usage
The strncasecmp () function compares two strings.
The strncasecmp () function compares the first n characters of a string (Case Insensitive ).

This function returns:
This function returns the following values:

0-if the two strings are equal
0-if the string is equal
<0-If string1 is less than string2
<0-If string1 is less string2
0-If string1 is greater than string2
0-If string1 is greater than string2
syntax
strncasecmp (string1, string2, length)

Parameter Parameters Description
String1 Required. specifies the first string to compare
Required parameter. Specifies the first string object involved in the comparison
String2 Required. specifies the second string to compare
Required parameter. Specify the second string object involved in the comparison
Length Required. specify the number of characters from each string to be used in the comparison
Required parameter. Specify the number of characters for Parameter comparison in each string

Tips and notes
Notes
Note: The strncasecmp () is binary safe and case-insensitive.
Note: The strncasecmp () function is binary accurate and case-insensitive.

Example
CaseCopyCodeThe Code is as follows: <? PHP
Echo strncasecmp ("Hello world! "," Hello Earth! ", 6 );
?>

The output of the code above will be:
The above code will output the following results:
0

Related Article

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.