当前位置: 首页 > 图文教程 > 网络编程 > PHP > php 中的str_replace 函数总结

PHP
PHP动态网页技术中SESSION的应用
PHP和MySQL操作应该注意的一些细节
理解学习PHP编码规范之注释和文件结构
PHP网站开发过程中注意这些安全知识
利用PHP自定义错误处理器处理出错信息
用PHP程序计算时间差的几种方法
PHP关于中文汉字替换与模式匹配的问题
PHP中的一些常识:类篇
PHP程序员一般都忽略了的几点精华
PHP4之COOKIE支持详解
用新PHP插件实现MySQL为基础的事务
如何利用PHP操纵Oracle LOB类型数据
PHP5 OOP编程之代理与定制异常(1)
PHP5 OOP编程之代理与定制异常(2)
用PHP编程语言开发动态WAP页面
PHP 编码规范-注释
用PHP正则表达式清除字符串的空白
PHP应用技巧:如何将代码中的通知和警告删除
PHP入门进阶学习必备的知识及工具
用PHP编程语言开发动态WAP页

PHP 中的 php 中的str_replace 函数总结


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 126 ::
收藏到网摘: n/a

格式:
[@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>'));