PHP strncasecmp字符串比較的小技巧
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ù)量 |
注意點(diǎn)
Note: The strncasecmp() is binary safe and case-insensitive.
注意:strncasecmp()函數(shù)是二進(jìn)制精確的,并且它不區(qū)分字母大小寫。
Example
案例
<?php
echo strncasecmp("Hello world!","hello earth!",6);
?>
The output of the code above will be:
上述代碼將輸出下面的結(jié)果:
0
- PHP學(xué)習(xí)之字符串比較和查找
- PHP 字符串分割和比較
- 用PHP中的 == 運(yùn)算符進(jìn)行字符串比較
- PHP容易被忽略而出錯(cuò)陷阱 數(shù)字與字符串比較
- php 字符串壓縮方法比較示例
- php實(shí)現(xiàn)比較兩個(gè)字符串日期大小的方法
- PHP字符串比較函數(shù)strcmp()和strcasecmp()使用總結(jié)
- PHP similar_text 字符串的相似性比較函數(shù)
- php常用字符串比較函數(shù)實(shí)例匯總
- php比較相似字符串的方法
- php比較兩個(gè)字符串長度的方法
- php字符串比較函數(shù)用法小結(jié)(strcmp,strcasecmp,strnatcmp及strnatcasecmp)
相關(guān)文章
執(zhí)行、獲取遠(yuǎn)程代碼返回:file_get_contents 超時(shí)處理的問題詳解
本篇文章是對(duì)執(zhí)行、獲取遠(yuǎn)程代碼返回:file_get_contents 超時(shí)處理的問題進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06PHP提示Deprecated: mysql_connect(): The mysql extension is dep
這篇文章主要介紹了PHP提示Deprecated: mysql_connect(): The mysql extension is deprecated的解決方法,是在進(jìn)行PHP數(shù)據(jù)庫程序開發(fā)中常會(huì)遇到的錯(cuò)誤,需要的朋友可以參考下2014-08-08php統(tǒng)計(jì)文件大小,以GB、MB、KB、B輸出
學(xué)習(xí)了fread讀取命令,用到了filesize函數(shù),知道這個(gè)函數(shù)定大有用到的時(shí)候,遂用之編寫統(tǒng)計(jì)文件大小函數(shù)2011-05-05解決FastCGI 進(jìn)程超過了配置的活動(dòng)超時(shí)時(shí)限的問題
本篇文章是對(duì)解決FastCGI 進(jìn)程超過了配置的活動(dòng)超時(shí)時(shí)限的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-07-07PHP基于php_imagick_st-Q8.dll實(shí)現(xiàn)JPG合成GIF圖片的方法
這篇文章主要介紹了PHP基于php_imagick_st-Q8.dll實(shí)現(xiàn)JPG合成GIF圖片的方法,需要的朋友可以參考下2014-07-07PHP實(shí)現(xiàn)負(fù)載均衡下的session共用功能
這篇文章主要介紹了PHP實(shí)現(xiàn)負(fù)載均衡下的session共用功能,結(jié)合實(shí)例形式分析了php基于memcache的session共享相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-04-04PHP調(diào)用Webservice實(shí)例代碼
NuSoap是PHP環(huán)境下的WebService編程工具,用于創(chuàng)建或調(diào)用WebService。2011-07-07