DOMXPath does not print HTML-source in <pre><code>...</code></pre> on screen

时间:2017-06-12 16:52:23

标签: php domxpath

I created a little script that modifies the output of a specific page:

foreach ( $markers as $marker ) {
    foreach ( $xpath->query( '//text()[not(ancestor::a) and not(ancestor::pre) and not(ancestor::code) and not(ancestor::img) and not(ancestor::script) and not(ancestor::style)]' ) as $node ) {
        $text     = $marker[ 'text' ];
        $url      = $marker[ 'url' ];
        $tip      = strip_tags( $marker[ 'excerpt' ] );
        $tooltip  = ( $tooltip ? "data-uk-tooltip='' title='$tip'" : "" );
        $replaced = preg_replace(
            '/\b' . preg_quote( $text, "/" ) . '\b/i',
            "<a href='$url' $hrefclass target='$target' $tooltip>\$0</a>",
            $node->wholeText
        );
        $newNode  = $dom->createDocumentFragment();
        $newNode->appendXML( $replaced );
        $node->parentNode->replaceChild( $newNode, $node );
    }
}

$event->setContent( utf8_encode( $dom->saveHTML()) );

Now I got a problem:

If the page-content contains a <pre><code><strong>some-content</strong></code></pre> the elements are not displayed as text - they are getting parsed as source code.

So it does not display the text <strong>some-content</strong> but parses it and displays some-content

1 个答案:

答案 0 :(得分:0)

Wrap your XML output in a textarea.

<textarea style="width:100%; height:150px">
<strong>
some-content
</strong>
</textarea>