PHP strncasecmp字符串比較的小技巧
更新時間:2011年01月04日 23:00:21 作者:
好象,大多時候,我們對于字符串的比較都是采用了 strtolower或者strtoupper之后,再判斷是否相等。
只是這樣就需要轉換兩次。大多時候,我們是針對字符集轉換的時候才會這樣,比如判斷參數傳進來是否utf-8,這5個字符的寫法,可就多了,比如UTF-8,Utf-8,utf-8等,那我們怎么辦呢?strtolower?strupper?不需要啦。。
strncasecmp($a,$b,$length)就可以了。。
如果返回是0則相等,那我們怎么判斷呢?
strncasecmp($str,'utf-8',5) == 0那么,傳入的參數就是utf8的,是否很方便呢?
只是這些函數我們平時不太用得到,我看到這個函數的用法卻是在 yii framework,他在處理事件的時候,判斷前兩個字符是否為 on 的時候,就是這樣判斷的。我也因此學到了一招。
strncasecmp Definition and Usage
定義和用法
The strncasecmp() function compares two strings.
strncasecmp()函數的作用是:比較字符串的前n個字符(大小寫不敏感)。
This function returns:
這個函數將返回下列值:
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)
Tips and Notes
注意點
Note: The strncasecmp() is binary safe and case-insensitive.
注意:strncasecmp()函數是二進制精確的,并且它不區(qū)分字母大小寫。
Example
案例
<?php
echo strncasecmp("Hello world!","hello earth!",6);
?>
The output of the code above will be:
上述代碼將輸出下面的結果:
0
strncasecmp($a,$b,$length)就可以了。。
如果返回是0則相等,那我們怎么判斷呢?
strncasecmp($str,'utf-8',5) == 0那么,傳入的參數就是utf8的,是否很方便呢?
只是這些函數我們平時不太用得到,我看到這個函數的用法卻是在 yii framework,他在處理事件的時候,判斷前兩個字符是否為 on 的時候,就是這樣判斷的。我也因此學到了一招。
strncasecmp Definition and Usage
定義和用法
The strncasecmp() function compares two strings.
strncasecmp()函數的作用是:比較字符串的前n個字符(大小寫不敏感)。
This function returns:
這個函數將返回下列值:
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參數 | 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 必要參數。指定每個字符串中參數比較的字符數量 |
注意點
Note: The strncasecmp() is binary safe and case-insensitive.
注意:strncasecmp()函數是二進制精確的,并且它不區(qū)分字母大小寫。
Example
案例
復制代碼 代碼如下:
<?php
echo strncasecmp("Hello world!","hello earth!",6);
?>
The output of the code above will be:
上述代碼將輸出下面的結果:
0
相關文章
執(zhí)行、獲取遠程代碼返回:file_get_contents 超時處理的問題詳解
本篇文章是對執(zhí)行、獲取遠程代碼返回:file_get_contents 超時處理的問題進行了詳細的分析介紹,需要的朋友參考下2013-06-06PHP提示Deprecated: mysql_connect(): The mysql extension is dep
這篇文章主要介紹了PHP提示Deprecated: mysql_connect(): The mysql extension is deprecated的解決方法,是在進行PHP數據庫程序開發(fā)中常會遇到的錯誤,需要的朋友可以參考下2014-08-08PHP基于php_imagick_st-Q8.dll實現JPG合成GIF圖片的方法
這篇文章主要介紹了PHP基于php_imagick_st-Q8.dll實現JPG合成GIF圖片的方法,需要的朋友可以參考下2014-07-07