页面导航: 首页网络编程PHP编程php教程 → 正文内容

php 中的str_replace 函数总结

发布:dxy 字体:[增加 减小] 类型:转载
格式:
[@str_replace("要替换的旧内容", "要取代原内容的新字符", $被替换内容的变量名)]
[@str_replace(array('旧1','旧2','旧3'), array('新1','新2','新3'), $被替换内容的变量名)]
[@str_replace(array('旧1','旧2','旧3'), '新内容', $被替换内容的变量名)]
实例:
多对一替换:想把内容字段里所有的<p></p>标签清除掉,替换成空

[@str_replace(array('<p>','</p>'), '', $Content)]
一对一替换:想把内容字段里所有的<br>标签换成<p>

[@str_replace('<br>', '<p>', $Content)]
多对多替换:想把内容字段里的<br>换成<br />, 同时<p>换<hr>,把</p>全清除

[@str_replace(array('<br>', '<p>','</p>'), array('<br />','<hr>',''), $Content)]

<?php
// Provides: <body text='black'>
$bodytag = str_replace("%body%", "black", "<body text='%body%'>");
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="<p>this is test</p><br><p size=3>this.</p><p>hello</p>";
print str_replace(array('<p>','</p>','<br>'),array('<br>','<hr>',''),$str)
?>

php 的sub_replace函数对应的js函数为
String.prototype.replaceAll = function(search, replace){
var regex = new RegExp(search, "g");
return this.replace(regex, replace);

var str = '<p>asfdafd</p><p>asfdafd</p><p>asfdafd</p>';
alert(str.replaceAll('</p>', '<br>')); 
浏览次数:载入中... 打印本文关闭本文返回首页
·在百度中搜索关于“php 中的str_replace 函数总结”相关内容
·在谷歌中搜索关于“php 中的str_replace 函数总结”相关内容

文章评论

共有 位脚本之家网友发表了评论我来说两句

同 类 文 章
最 近 更 新
热 点 排 行