Home  >  Article  >  Backend Development  >  php里的str_replace函数可以与此同时替换3处吗

php里的str_replace函数可以与此同时替换3处吗

WBOY
WBOYOriginal
2016-06-13 12:10:051349browse

php里的str_replace函数可以同时替换3处吗?
$content = str_replace('#p#副标题#e#','',$body);
$content = str_replace('请看下一页', '',$body);
$content = str_replace('/uploads', 'http://www.****.com/uploads',$body);

各位,我想输出的$body读取的内容,同时替换掉里面的“#p#副标题#e#”  “'请看下一页”  “/uploads”

请问怎么实现呀?现在只能替换最后一个语句的,3个不能同时被替换
------解决思路----------------------

$body =<<< 'TXT'<br />各位,我想输出的$body读取的内容,同时替换掉里面的“#p#副标题#e#”  “'请看下一页”  “/uploads”<br />TXT;<br /><br />$body = str_replace('#p#副标题#e#','',$body);<br />$body = str_replace('请看下一页', '',$body);<br />$body = str_replace('/uploads', 'http://www.****.com/uploads',$body);<br />echo $body;<br />
写成这样更简捷
body =<<< 'TXT'<br />各位,我想输出的$body读取的内容,同时替换掉里面的“#p#副标题#e#”  “'请看下一页”  “/uploads”<br />TXT;<br />$p = array('#p#副标题#e#', '请看下一页', '/uploads');<br />$r = array('','', 'http://www.****.com/uploads');<br />echo str_replace($p, $r, $body);

------解决思路----------------------
<br />header("Content-type: text/html; charset=utf-8");<br />$body = <<<BODY<br />#p#副标题#e#<br />请看下一页<br />/uploads<br />BODY;<br />//$content = str_replace('#p#副标题#e#','',$body);<br />//$content = str_replace('请看下一页', '',$body);<br />//$content = str_replace('/uploads', 'http://www.****.com/uploads',$body);<br />echo "替换前的内容:" . "<br/>" . $body . "<br/>" . "<hr/>";<br />$arr_orignal_content = array ('#p#副标题#e#', '请看下一页', '/uploads');<br />$arr_replace_content = array ('', '', 'http://www.baidu.com/uploads');<br />$body = str_replace($arr_orignal_content, $arr_replace_content, $body);<br />echo "替换后的内容:" . "<br/>" . $body;<br />

------解决思路----------------------
楼上的都是正解
------解决思路----------------------
可以用正则,把这三个拼成一个正则即可
------解决思路----------------------
第一次替換完后,就不要用$body了,而是用$content進行后面的替換。

<br />$content = str_replace('#p#副标题#e#','',$body);<br />$content = str_replace('请看下一页', '',$content);<br />$content = str_replace('/uploads', 'http://www.****.com/uploads',$content);<br />

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn