php preg_replace '</p>' 为 '<br />'?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5294209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 20:53:46 来源:igfitidea点击:
preg_replace '</p>' with '<br />'?
提问by Jared
I have my code removing the <p>
starting tags, but now I want to replace the ending </p>
tags with line breaks. How can I do this?
我的代码删除了<p>
起始标记,但现在我想</p>
用换行符替换结束标记。我怎样才能做到这一点?
This is what I have:
这就是我所拥有的:
$content = 'This is the content';
$newcontent = preg_replace("/<p[^>]*?>", "", $content);
$newcontent = preg_replace("</p>", "<br />", $newcontent);
回答by Richard Rodriguez
use str_replace instead of preg_replace, so:
使用 str_replace 而不是 preg_replace,所以:
$content = '<p>This is a new content for missing slash</p>';
$newcontent = preg_replace("/<p[^>]*?>/", "", $content);
$newcontent = str_replace("</p>", "<br />", $newcontent);
回答by Eric Grivilers
$content = preg_replace('#<p(.*?)>(.*?)</p>#is', '<br/>', $content);