Home > Article > Backend Development > PHP strncasecmp string comparison tips_PHP tutorial
Just do this and you need to convert it twice. Most of the time, we do this when we are converting character sets, such as judging whether the parameters passed in are utf-8. There are many ways to write these 5 characters, such as UTF-8, Utf-8, utf-8, etc. , then what should we do? strtolower? strupper? No need. .
strncasecmp($a,$b,$length) will do. .
If the return value is 0, it means they are equal, so how do we judge?
strncasecmp($str,'utf-8',5) == 0 Then, the parameter passed in is utf8. Is it convenient?
It’s just that we don’t usually use these functions. I saw the usage of this function in yii framework. When it processes events, it determines whether the first two characters are on. This is how it is judged. I also learned a trick from this.
strncasecmp Definition and Usage
Definition and Usage
The strncasecmp() function compares two strings. The function of the strncasecmp() function is to compare the first n characters of the string (upper and lower case insensitive).
This function returns:
This function will return the following values:
0 - if the two strings are equal
0 - If the strings are 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
Syntax
strncasecmp(string1,string2,length)
Parameter parameter | Description | ||||||||
---|---|---|---|---|---|---|---|---|---|
string1 | Required. Specifies the first string to compare Required parameter. Specify the first string object to be compared |
||||||||
string2 | Required. Specifies the second string to compare
|
||||||||
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 |