Home  >  Article  >  Backend Development  >  PHP strncasecmp string comparison tips_PHP tutorial

PHP strncasecmp string comparison tips_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:33:12918browse

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
Parameter参数 Description描述
string1 Required. Specifies the first string to compare
必要参数。指定参与比较的第一个字符串对象
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 second string object to be compared
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 exact, and it is not case sensitive.

Example
Case
Copy code
The code is as follows:


echo strncasecmp("Hello world!","hello earth!",6);
?>


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

http://www.bkjia.com/PHPjc/322684.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322684.htmlTechArticleJust like this, 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...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn