php字串比較

來源:互聯網
上載者:User

1、按位元組進行字串比較:

strcmp()和strcasecmp()函數用於對兩個字串按位元組進行比較。

文法格式:

int strcmp(string str1,string str2)

int strcasecmp(string str1,string str2)

如果str1=str2,傳回值為0;如果str1>str2,傳回值>0;如果str1<str2,傳回值小於0

這兩個函數的區別是:strcmp()區分字元的大小寫;而strcasecmp()不區分字元的大小寫

2、按自然排序法進行字串比較: strmatcmp()函數

文法:

int strnatcmp(string str1,string str2)

如果字串相等,返回0;如果str1>str2,傳回值大於0;如果str1<str2,傳回值小於0

3、指定從源字串的位置進行比較:strncmp()函數

文法:

int strncmp(string str1,string str2,int len)

說明:

str1: 指定參與比較的第一個字串

str2: 指定參與比較的第二個字串

len: 指定每個字串中參與比較字元的數量

如果字串相等,返回0;如果str1>str2,傳回值>0;如果str1<str2,傳回值<0

例:比較兩個字串的前三個字元是否相等

<?php
$a="www.bianceng.cn";
$b="www.bianceng.com";
echo strncmp($a,$b,3);
?>

聯繫我們

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