且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用PHP从字符串中删除html元素

更新时间:2023-02-21 19:21:23

如果您对非正则表达式解决方案感兴趣,也可以:

<?php
    $text = "<p>This is some example text This is some example text This is some example text</p>
             <p><em>This is some example text This is some example text This is some example text</em></p>
             <p>This is some example text This is some example text This is some example text</p>";


    $emStartPos = strpos($text,"<em>");
    $emEndPos = strpos($text,"</em>");

    if ($emStartPos && $emEndPos) {
        $emEndPos += 5; //remove <em> tag aswell
        $len = $emEndPos - $emStartPos;

        $text = substr_replace($text, '', $emStartPos, $len);
    }

?>

这将删除标记之间的所有内容。