欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP strncasecmp字符串比較的小技巧

 更新時(shí)間:2011年01月04日 23:00:21   作者:  
好象,大多時(shí)候,我們對(duì)于字符串的比較都是采用了 strtolower或者strtoupper之后,再判斷是否相等。
只是這樣就需要轉(zhuǎn)換兩次。大多時(shí)候,我們是針對(duì)字符集轉(zhuǎn)換的時(shí)候才會(huì)這樣,比如判斷參數(shù)傳進(jìn)來是否utf-8,這5個(gè)字符的寫法,可就多了,比如UTF-8,Utf-8,utf-8等,那我們?cè)趺崔k呢?strtolower?strupper?不需要啦。。
strncasecmp($a,$b,$length)就可以了。。
如果返回是0則相等,那我們?cè)趺磁袛嗄兀?
strncasecmp($str,'utf-8',5) == 0那么,傳入的參數(shù)就是utf8的,是否很方便呢?
只是這些函數(shù)我們平時(shí)不太用得到,我看到這個(gè)函數(shù)的用法卻是在 yii framework,他在處理事件的時(shí)候,判斷前兩個(gè)字符是否為 on 的時(shí)候,就是這樣判斷的。我也因此學(xué)到了一招。

strncasecmp Definition and Usage
定義和用法
The strncasecmp() function compares two strings.
strncasecmp()函數(shù)的作用是:比較字符串的前n個(gè)字符(大小寫不敏感)。

This function returns:
這個(gè)函數(shù)將返回下列值:

0 - if the two strings are equal
0 – 如果字符串相等
<0 - if string1 is less than string2
<0 – 如果string1小于string2
>0 - if string1 is greater than string2
>0 – 如果string1大于string2
Syntax
語法
strncasecmp(string1,string2,length)
Parameter參數(shù) Description描述
string1 Required. Specifies the first string to compare
必要參數(shù)。指定參與比較的第一個(gè)字符串對(duì)象
string2 Required. Specifies the second string to compare
必要參數(shù)。指定參與比較的第二個(gè)字符串對(duì)象
length Required. Specify the number of characters from each string to be used in the comparison
必要參數(shù)。指定每個(gè)字符串中參數(shù)比較的字符數(shù)量
Tips and Notes
注意點(diǎn)
Note: The strncasecmp() is binary safe and case-insensitive.
注意:strncasecmp()函數(shù)是二進(jìn)制精確的,并且它不區(qū)分字母大小寫。

Example
案例
復(fù)制代碼 代碼如下:

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

The output of the code above will be:
上述代碼將輸出下面的結(jié)果:
0

相關(guān)文章

最新評(píng)論