微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

嗨,在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>

我需要在< div class =“c-fc c-bc”id =“content”>之间替换everthing.和< / 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);

我错过了什么吗?

解决方法:

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

preg_replace('/(.*)<\/div>/s','abc',$body);

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐