七:字串截取函數 :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教程有興趣的朋友有所協助。