Jonny Barnes
Jonny Barnes

Reputation: 535

edit html text with domdocument

Given a partial HTML fragment such as

<div id="article">
    <p>Some text</p>
    <p>More text</p>
    <pre><code>
         echo $variable;
    </code></pre>
</div>

I want to loop through the text nodes and apply a function to them, doesn't really matter what the function does, but not if its the text in the pre code block.

Given it's a partial code block I think I need to use DOMDocument->createDocumentFragment(). But how can I loop through the text nodes then save the output without extra <html> tags being created, as saveHTML() seems to do by default?

Upvotes: 0

Views: 99

Answers (2)

salathe
salathe

Reputation: 51950

You could use a document fragment. Looping over the text nodes is the easy part, finding them is a little tricker. For that, the example below uses an XPath query to find all text nodes that are not descendants of the <pre> element.

$doc = new DOMDocument;
$xpath = new DOMXPath($doc);

$frag = $doc->createDocumentFragment();
$frag->appendXML(<<<'HTML'
<div id="article">
    <p>Some text</p>
    <p>More text</p>
    <pre><code>
         echo $variable;
    </code></pre>
</div>
HTML
);

$text_nodes = $xpath->query('descendant::text()[not(ancestor::pre)]', $frag);
foreach ($text_nodes as $text_node) {
    $text_node->data = strrev($text_node->data);
}

// Print the div
echo $doc->saveHTML($frag->firstChild);

This will print out something like:

<div id="article">    
<p>txet emoS</p>    
<p>txet eroM</p>    
<pre><code>
         echo $variable;
    </code></pre>
</div>

Helpful links:

Upvotes: 1

David
David

Reputation: 4767

Why are you using DomDocument?

Why not read the HTML from the 'remote source' and then save it as a file on the server using file_put_contents()

http://php.net/manual/en/function.file-put-contents.php

Upvotes: 0

Related Questions