PHP strncasecmp字串比較的小技巧_PHP教程

來源:互聯網
上載者:User
只是這樣就需要轉換兩次。大多時候,我們是針對字元集轉換的時候才會這樣,比如判斷參數傳進來是否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)
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
必要參數。指定每個字串中參數比較的字元數量
Tips and Notes
注意點
Note: The strncasecmp() is binary safe and case-insensitive.
注意:strncasecmp()函數是二進位精確的,並且它不區分字母大小寫。

Example
案例
複製代碼 代碼如下:
echo strncasecmp("Hello world!","hello earth!",6);
?>

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

http://www.bkjia.com/PHPjc/322684.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/322684.htmlTechArticle只是這樣就需要轉換兩次。大多時候,我們是針對字元集轉換的時候才會這樣,比如判斷參數傳進來是否utf-8,這5個字元的寫法,可就多了...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.