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

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

WBOY
WBOYOriginal
2016-06-23 13:45:041083browse

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

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

楼上的都是正解

可以用正则,把这三个拼成一个正则即可

第一次替?完后,就不要用$body了,而是用$content?行后面的替?。

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

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