str_replace
时间:2007-07-26 来源:rainbutterfly
[color="#0000bb"]
字符串取代。
[color="#ff8000"]语法: string str_replace(string needle, string str, string haystack);
[color="#ff8000"]返回值: 字符串
[color="#ff8000"]函数种类: 资料处理
[color="#ff8000"]内容说明
本函数将字符串 str 代入 haystack 字符串中,将所有的 needle 置换成 str。[email protected]
(11-Apr-1999) 指出在 PHP 3.0.7 版,本函数有些 bug,而 [email protected] (05-Jun-1999)
补充在 PHP 3.0.8 版本函数就回复正常了。
[color="#ff8000"]使用范例
[color="#000000"]下例将 %body% 以 black 取代
"[color="#007700"]);
echo [color="#0000bb"]$bodytag[color="#007700"];
[color="#0000bb"] >
格式:
[@str_replace("要替换的旧内容", "要取代原内容的新字符", $被替换内容的变量名)]
[@str_replace(array('旧1','旧2','旧3'), array('新1','新2','新3'), $被替换内容的变量名)]
[@str_replace(array('旧1','旧2','旧3'), '新内容', $被替换内容的变量名)]
实例:
多对一替换:想把内容字段里所有的标签清除掉,替换成空
[@str_replace(array('',''), '', $Content)]
一对一替换:想把内容字段里所有的
标签换成
[@str_replace('
', '', $Content)]
多对多替换:想把内容字段里的
换成
, 同时换,把全清除
[@str_replace(array('
', '',''), array('
','',''), $Content)]
$bodytag = str_replace("%body%", "black", "");
echo $bodytag;
// Provides: Hll Wrld f PHP
$vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U");
$onlyconsonants = str_replace($vowels, "", "Hello World of PHP");
echo $onlyconsonants;
// Provides: You should eat pizza, beer, and ice cream every day
$phrase = "You should eat fruits, vegetables, and fiber every day.";
$healthy = array("fruits", "vegetables", "fiber");
$yummy = array("pizza", "beer", "ice cream");
$newphrase = str_replace($healthy, $yummy, $phrase);
echo $newphrase;
// Use of the count parameter is available as of PHP 5.0.0
$str = str_replace("ll", "", "good golly miss molly!", $count);
echo $count; // 2
$str="this is test
this.hello";
print str_replace(array('','','
'),array('
','',''),$str)
>
php 的sub_replace函数对应的js函数为
String.prototype.replaceAll = function(search, replace){
var regex = new RegExp(search, "g");
return this.replace(regex, replace);
}
var str = 'asfdafdasfdafd<
相关阅读 更多 +
排行榜 更多 +