Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to saveHTML of DOMDocument without HTML wrapper?
    primarykey
    data
    text
    <p>I'm the function below, I'm struggling to output the DOMDocument without it appending the XML, HTML, <em>body</em> and <em>p</em> tag wrappers before the output of the content. The suggested fix:</p> <pre><code>$postarray['post_content'] = $d-&gt;saveXML($d-&gt;getElementsByTagName('p')-&gt;item(0)); </code></pre> <p>Only works when the content has no block level elements inside it. However, when it does, as in the example below with the h1 element, the resulting output from saveXML is truncated to...</p> <blockquote> <p>&lt;p>If you like&lt;/p></p> </blockquote> <p>I've been pointed to this post as a possible workaround, but I can't understand how to implement it into this solution (see commented out attempts below).</p> <p>Any suggestions?</p> <pre><code>function rseo_decorate_keyword($postarray) { global $post; $keyword = "Jasmine Tea" $content = "If you like &lt;h1&gt;jasmine tea&lt;/h1&gt; you will really like it with Jasmine Tea flavors. This is the last ocurrence of the phrase jasmine tea within the content. If there are other instances of the keyword jasmine tea within the text what happens to jasmine tea." $d = new DOMDocument(); @$d-&gt;loadHTML($content); $x = new DOMXpath($d); $count = $x-&gt;evaluate("count(//text()[contains(translate(., 'ABCDEFGHJIKLMNOPQRSTUVWXYZ', 'abcdefghjiklmnopqrstuvwxyz'), '$keyword') and (ancestor::b or ancestor::strong)])"); if ($count &gt; 0) return $postarray; $nodes = $x-&gt;query("//text()[contains(translate(., 'ABCDEFGHJIKLMNOPQRSTUVWXYZ', 'abcdefghjiklmnopqrstuvwxyz'), '$keyword') and not(ancestor::h1) and not(ancestor::h2) and not(ancestor::h3) and not(ancestor::h4) and not(ancestor::h5) and not(ancestor::h6) and not(ancestor::b) and not(ancestor::strong)]"); if ($nodes &amp;&amp; $nodes-&gt;length) { $node = $nodes-&gt;item(0); // Split just before the keyword $keynode = $node-&gt;splitText(strpos($node-&gt;textContent, $keyword)); // Split after the keyword $node-&gt;nextSibling-&gt;splitText(strlen($keyword)); // Replace keyword with &lt;b&gt;keyword&lt;/b&gt; $replacement = $d-&gt;createElement('strong', $keynode-&gt;textContent); $keynode-&gt;parentNode-&gt;replaceChild($replacement, $keynode); } $postarray['post_content'] = $d-&gt;saveXML($d-&gt;getElementsByTagName('p')-&gt;item(0)); // $postarray['post_content'] = $d-&gt;saveXML($d-&gt;getElementsByTagName('body')-&gt;item(1)); // $postarray['post_content'] = $d-&gt;saveXML($d-&gt;getElementsByTagName('body')-&gt;childNodes); return $postarray; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload