doujiexin1136 2014-12-14 15:43
浏览 163
已采纳

如何删除字符串中的元素内容,只留下最外层的元素标签?

I have a string like this:

<p>
This is some text
</p>

<p>
This is some text
</p>
<p>
This is some text
</p>

<blockquote data-id="1">
    This is some text

    <blockquote data-id="2">
        This is some text
    </blockquote>
</blockquote>

<blockquote data-id="3">
    <blockquote data-id="4">
        This is some text

        <blockquote data-id="5">
            This is some text
        </blockquote>
    </blockquote>
    This is some text
</blockquote>

<blockquote data-id="6">
    This is some text
</blockquote>

I want to keep the outermost blockquote tags, but delete the contents. So I want to convert the above to this:

<p>
This is some text
</p>

<p>
This is some text
</p>
<p>
This is some text
</p>

<blockquote data-id="1"></blockquote>

<blockquote data-id="3"></blockquote>

<blockquote data-id="6"></blockquote>

What is an efficient way to do this in PHP?

  • 写回答

2条回答 默认 最新

  • dongsu4345 2014-12-14 16:35
    关注

    Many ways to skin this cat. I'd give the string a dummy root node, ditch all nodes matching the xpath expression /root/blockquote/text() | /root/blockquote/*, then rebuild the string from the root's children.


    Example:

    $string = <<<'STRING'
    <p>
    This is some text
    </p>
    
    <p>
    This is some text
    </p>
    <p>
    This is some text
    </p>
    
    <blockquote data-id="1">
        This is some text
    
        <blockquote data-id="2">
            This is some text
        </blockquote>
    </blockquote>
    
    <blockquote data-id="3">
        <blockquote data-id="4">
            This is some text
    
            <blockquote data-id="5">
                This is some text
            </blockquote>
        </blockquote>
        This is some text
    </blockquote>
    
    <blockquote data-id="6">
        This is some text
    </blockquote>
    STRING;
    
    $dom = new DOMDocument();
    $dom->loadXML("<root>$string</root>");
    $xpath = new DOMXPath($dom);
    
    foreach ($xpath->query('/root/blockquote/text() | /root/blockquote/*') as $node) {
        $node->parentNode->removeChild($node);
    }
    
    $string = '';
    foreach ($dom->documentElement->childNodes as $node) {
        $string .= $dom->saveHTML($node);
    }
    
    echo $string;
    

    Output:

    <p>
    This is some text
    </p>
    
    <p>
    This is some text
    </p>
    <p>
    This is some text
    </p>
    
    <blockquote data-id="1"></blockquote>
    
    <blockquote data-id="3"></blockquote>
    
    <blockquote data-id="6"></blockquote>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?