php字串函數(3)

來源:互聯網
上載者:User
七:字串截取函數 :str_replace(find,replace,string,count);
substr_replace(string,replace,start,length);

$msg = "hello,word I love php";$rs = substr_replace($msg,"mysql",-3,3);echo$rs."
";$rsl = str_replace("word", "php", $msg);echo$rsl;

如所示,substr_replace(string,replace,start,length);主要針對字串裡的位置進行的替換。string是
所尋找的字串,replace是要進行替換的字元,start是替換開始的位置(若為正數,從左開始尋找。為負數,從右開始尋找),length(可選。若不選擇,則表示把開始的位置後的全部字元替換)表示要替換的長度。

str_replace(find,replace,string,count); find表示要進行替換的字元。replace表示要被替換的字元。string表示要尋找的字串。count表示執行的次數(可選)。此函數對大小寫敏感。對大小寫不敏感的str_ireplace();用法和str_replace()是相同的。

八:比較字串函數 strcmp(str1,str2). strcasecmp(str1,str2);

$msg1 = "hello";    $msg2 = "HELLO";    echo strcmp($msg1, $msg2)."
"; echo strcasecmp($msg1 ,$msg2);

結果如。兩個函數的區別是strcmp()是小寫敏感的,strcasecmp()對大小寫不敏感。
當比較的字元相同時,傳回值為0. 當str1 > str2時,傳回值大於0。
當str1 < str2時,傳回值小於0。

九:字串大小寫轉換 strtolower (); strtoupper (); ucfirst(); ucwords();

$str = "I AM PETAL";echo strtolower($str)."
"; //大寫轉換為小寫$stra = "i am petal";echo strtoupper($stra)."
"; // 小衛轉換為大寫echo ucfirst($stra)."
"; //只將字串的第一個字元轉換為大寫echo ucwords($stra); //將字串每一個單詞的首字母轉換為大寫

得出結果如下

以上就介紹了php字串函數(3),包括了方面的內容,希望對PHP教程有興趣的朋友有所協助。

  • 聯繫我們

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