从表中删除p标签

时间:2014-05-20 10:18:04

标签: php

如何从所有p和单元格中删除所有tables代码,并将其替换为br代码?

所以这......

<table>
    <tbody>
        <tr>
            <td>1.</td>
            <td>Lorem ipsum dolor sit amet</td>
            <td>
                <p>
                Sed ut perspiciatis unde omnis iste natus error sit voluptatem
                <a class="" style="" href="www.accusantiumdoloremque.co.uk">www.accusantiumdoloremque.co.uk</a>
                </p>
                <p>Laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore ve</p>
            </td>
        </tr>
    </tbody>
</table>

......会变成这个......

<table>
    <tbody>
        <tr>
            <td>1.</td>
            <td>Lorem ipsum dolor sit amet</td>
            <td>Sed ut perspiciatis unde omnis iste natus error sit voluptatem
                <br />
                <a class="" style="" href="www.accusantiumdoloremque.co.uk">www.accusantiumdoloremque.co.uk</a>
                <br />
                <br />
                Laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore ve
                <br />
                    <br />
            </td>
        </tr>
    </tbody>
</table>

我猜我会用这样的东西:

$mycontent = str_replace('<p>', '', $mycontent );
$mycontent = str_replace('</p>', '<br /><br />', $mycontent );

但是我怎么只在tables和表格单元格上使用它,而不是任何其他内容?

2 个答案:

答案 0 :(得分:0)

只需使用jQuery。对所有td或tr元素使用选择器,你就完成了

答案 1 :(得分:0)

Try This. If there is more than one table you can put this code in a loop and use it.

    $contentarr= explode("</table>",$mycontent);
    $subcontent=$contentarr[0];
    $subcontentarr=explode("<table>",$subcontent);
    $data=$subcontentarr[1];
    $data=str_replace('<p>', '', $data );
    $data=str_replace('</p>', '<br /><br />', $data);
    $subcontentarr[1]=$data;
    $substr=implode("<table>",$subcontentarr);
    $contentarr[0]=$substr;
    $new_content=implode("</table>",$contentarr);