php中常用字串比較函數

來源:互聯網
上載者:User
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

//開原始碼phpfensi.com

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.