如何使用preg_replace替换多行上的文本

Rol*_*and 11 php

嗨,在html页面中有以下内容,可以拉伸多行

<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>
Run Code Online (Sandbox Code Playgroud)

我需要更换之间的寄托都<div class="c-fc c-bc" id="content"></div>自定义文本

我使用以下代码来完成此操作但如果它是多行则不想工作,但如果evertinh在一行中则有效

$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);
Run Code Online (Sandbox Code Playgroud)

我错过了什么吗?

Mar*_*ers 26

如果这不是HTML,我会告诉你使用DOTALL修饰符来改变."匹配除新行之外的所有内容"到"匹配所有内容" 的含义:

preg_replace('/(.*)<\/div>/s','abc',$body);
Run Code Online (Sandbox Code Playgroud)

但这是HTML,所以请改用HTML解析器.


小智 15

它是"s"旗,它启用.捕获换行符