substr_compare() 函數(shù)從指定的開始長度比較兩個字符串,該函數(shù)返回:
0 - 如果兩字符串相等,<0 - 如果 string1 (從開始位置)小于 string2,>0 - 如果 string1 (從開始位置)大于 string2.
語法:substr_compare(string1,string2,startpos,length,case),代碼如下:
- $str1="hello world"; //定義字符串1
- $str2="hello world"; //定義字符串2
- $result=substr_compare($str1,$str2,1,10); //執(zhí)行比較操作
- echo $result; //輸出結(jié)果,1
strnatcasecmp() 函數(shù)使用一種"自然"算法來比較兩個字符串,在自然算法中,數(shù)字 "2" 小于數(shù)字 "10",在計算機排序中,"2" 大于 "10",這是因為 "2" 大于 "10" 的第一個數(shù)字,代碼如下:
- $str1="hello world"; //定義字符串1
- $str2="hello world"; //定義字符串2
- $result=strnatcasecmp($str1,$str2); //執(zhí)行比較操作
- echo $result; //輸出結(jié)果,0
strncasecmp() 函數(shù)比較兩個字符串,該函數(shù)返回:
0 - 如果兩個字符串相等,<0 - 如果 string1 小于 string2,>0 - 如果 string1 大于 string2.
語法:strncasecmp(string1,string2,length),代碼如下:
- $str1="hello world"; //定義字符串1
- $str2="hello world"; //定義字符串2
- $result=strncasemp($str1,$str2,7); //執(zhí)行比較操作
- echo $result; //輸出結(jié)果,0
strncmp() 函數(shù)比較兩個字符串,該函數(shù)返回:
0 - 如果兩個字符串相等,<0 - 如果 string1 小于 string2,>0 - 如果 string1 大于 string2.
語法:strncmp(string1,string2,length),代碼如下:
- $str1="hello world"; //定義字符串1
- $str2="hello world"; //定義字符串2
- $result=strncmp($str1,$str2,7); //執(zhí)行比較操作
- echo $result; //輸出結(jié)果,1
strcoll() 函數(shù)比較兩個字符串,該函數(shù)返回:
0 - 如果兩個字符串相等,<0 - 如果 string1 小于 string2,>0 - 如果 string1 大于 string2.
字符串的比較會根據(jù)本地設(shè)置而變化,a<a 或 a>a.
語法:strcoll(string1,string2),代碼如下:
- $str1="hello world"; //定義字符串1
- $str2="hello world"; //定義字符串2
- $result=strcoll($str1,$str2); //執(zhí)行比較操作
- echo $result; //輸出結(jié)果,1
- //開源代碼Vevb.com
新聞熱點
疑難解答