Tips for comparing PHP strncasecmp strings

Source: Internet
Author: User
Tags character set comparison first string
It just needs to be converted two times. Most of the time, we are on the character set conversion time will be so, such as judging the parameters passed in whether Utf-8, the 5 characters of the writing, but more, such as utf-8,utf-8,utf-8, then how do we do? Strtolower?strupper? No need.
STRNCASECMP ($a, $b, $length) is OK.
If the return is equal to 0, how do we judge it?
STRNCASECMP ($str, ' Utf-8 ', 5) = 0 So, the incoming parameter is UTF8, is it convenient?
It's just that we don't use them very often, and I see that this function is used in the YII framework, and that's when he's dealing with an event, judging whether the first two characters are on. And so I learned a trick.

STRNCASECMP Definition and Usage
Definitions and usage
The strncasecmp () function compares two strings.
The strncasecmp () function is to compare the first n characters of a string (not case sensitive).

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 than string2
>0-if string1 is greater than string2
>0– if string1 is greater than string2
Syntax
Grammar
STRNCASECMP (String1,string2,length)
parameter Parameters Description Description
String1 Required. Specifies the "a" to compare
Necessary parameters. Specifies the first string object to participate in the comparison
string2 Required. Specifies the second string to compare
Necessary parameters. Specifies the second string object to participate in the comparison
Length Required. Specify the number of characters from each string to is used in the comparison
Necessary parameters. Specify the number of characters in each string to compare the parameters
Tips and Notes
Pay attention.
Note:the strncasecmp () is binary safe and case-insensitive.
Note: the STRNCASECMP () function is binary precise and does not differentiate between uppercase and lowercase letters.

Example
Case
Copy CodeThe code is as follows:
<?php
echo strncasecmp ("Hello world!", "Hello earth!", 6);
?>

The output of the code above would be:
The above code will output the following result:
0

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.