php - 如何从段落外删除分隔符

标签 php html

我想删除所有 <br> <p></p> 之外的标签.但是在<p></p>里面休息不应该受到伤害。我怎样才能在 php 中实现这个。下面是一个例子。

$html = "<br> <p> This is the Firs para <br> 
        This is a line after the first break <br>
        This is the line after the 2nd break <br>
        Here the first para ends </p>
        <br>
        <br>
        <p> This is the 2nd para <br> 
        This is a line after the first break in 2nd para <br>
        This is the line after the 2nd break <br>
        Here the 2nd para ends </p>"

我想要的结果如下

$html = "<p> This is the Firs para <br> 
        This is a line after the first break <br>
        This is the line after the 2nd break <br>
        Here the first para ends </p>
        <p> This is the 2nd para <br> 
        This is a line after the first break in 2nd para <br>
        This is the line after the 2nd break <br>
        Here the 2nd para ends </p>"

最佳答案

这对你有帮助。

$out = preg_replace("(<p(?:\s+\w+(?:=\w+|\"[^\"]+\"|'[^']+')?)*>.*?</p>(*SKIP)(*FAIL)"
."|<br>)is", "", $html);

echo $out;

关于php - 如何从段落外删除分隔符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45568398/

相关文章:

php - 文件存在但 laravel 找不到它(FileNotFoundException)

php - Laravel - 在插入之前检查数据库参数的有效方法

php - codeigniter 日期和时间在 View 中无法正确转换

jquery - 如何使用 jquery 添加拖放 div?

jQuery .toggle() 不适用于 IE 中的 TRs

php - Ajax 函数显示特定 div 内提交的结果

javascript - 是否可以将数据表作为 AJAX 数据发布到服务器?

php - ReCaptcha 在 iPhone 上无法正常工作

javascript - 使用 JavaScript 获得最高 margin

html - 目录更改后不显示横幅图像