php字串與字元替換函式

雲棲希望。發表於2017-12-04

php教程替換字元效率最高也是最簡單字元替換函式str_replace($arr1,$arr2,$str)
例項一

str_replace(“iwind”, “kiki”, “i love iwind, iwind said”);
將輸出 “i love kiki, kiki said”

結果

即將 原字串中的所有”iwind”都替換成了”kiki”.str_replace是大小寫敏感的,所以對你不能設想用 str_replace(“iwind”, “kiki”,…)替換原字串中的”iwind”. str_replace還可以實現多對一

定義和用法
str_replace() 函式使用一個字串替換字串中的另一些字元。

語法
str_replace(find,replace,string,count)引數 描述 
find 必需。規定要查詢的值。 
replace 必需。規定替換 find 中的值的值。 
string 必需。規定被搜尋的字串。 
count 可選。一個變數,對替換數進行計數。

下面用一款

//– 程式名稱:strreplace()
//– 程式用途:替換變數中的非法字元
//– 傳入引數:變數值
//********************************************************

   

 function strreplace($str){
$str = strips教程lashes($str);
$str = str_replace(chr(92),“,$str);
$str = str_replace(chr(47),“,$str);
$str = str_replace(chr(10).chr(13),”<br>”,$str);
$str = str_replace(`<`,”&lt;”,$str);

      $str = str_replace(`>`,”&gt;”,$str);
$str = str_replace(`;`,”;”,$str);
$str = str_replace(`”`,”“”,$str);
$str = str_replace(“`”,”‘”,$str);
$str = str_replace(” “,” “,$str);
$str = str_replace(“/**/”,” “,$str);

      return trim($str);
}

本文轉自部落格園知識天地的部落格,原文連結:php字串與字元替換函式,如需轉載請自行聯絡原博主。


相關文章