如何使用preg_replace替换多行文本 [英] How to replace text over multiple lines using preg_replace
本文介绍了如何使用preg_replace替换多行文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在延伸多行的html页面中有以下内容
Hi have the following content within an html page that stretches multiple lines
<div class="c-fc c-bc" id="content">
<span class="content-heading c-hc">Heading 1 </span><br />
The Home Page must provide a introduction to the services provided.<br />
<br />
<span class="c-sc">Sub Heading</span><br />
The Home Page must provide a introduction to the services provided.<br />
<br />
<span class="c-sc">Sub Heading</span><br />
The Home Page must provide a introduction to the services provided.<br />
</div>
我需要用自定义文本替换<div class="c-fc c-bc" id="content">
和</div>
之间的所有内容
I need to replace everthing between <div class="c-fc c-bc" id="content">
and </div>
with custom text
我使用以下代码来完成此操作,但是如果它是多行,则它不希望工作,但是如果evertinh在一行中,则它可以工作
I use the following code to accomplish this but it does not want to work if it's multiple lines, but works if evertinh is in one line
$body = file_get_contents('../../templates/'.$val['url']);
$body = preg_replace('/<div class=\"c\-fc c\-bc\" id=\"content\">(.*)<\/div>/','<div class="c-fc c-bc" id="content">abc</div>',$body);
我想念什么吗?
推荐答案
If this weren't HTML, I'd tell you to use the DOTALL modifier to change the meaning of .
from 'match everything except new line' to 'match everything':
preg_replace('/(.*)<\/div>/s','abc',$body);
但这是HTML,因此请改用HTML解析器.
But this is HTML, so use an HTML parser instead.
这篇关于如何使用preg_replace替换多行文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文