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