Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you dont mind a plain DOMDocument solution (DOMDocument is used unter the hood of phpQuery to parse the HTML fragments), I did something similiar a while ago. I adapted the code to do what you need:</p> <pre><code>$document = new DOMDocument(); // load html from file $document-&gt;loadHTMLFile('event.html'); // find all span elements in document $spanElements = $document-&gt;getElementsByTagname('span'); $spanElementsToReplace = array(); // use temp array to store span elements // as you cant iterate a NodeList and replace the nodes foreach($spanElements as $spanElement) { $spanElementsToReplace[] = $spanElement; } // create a p element, append the children of the span to the p element, // replace span element with p element foreach($spanElementsToReplace as $spanElement) { $p = $document-&gt;createElement('p'); foreach($spanElement-&gt;childNodes as $child) { $p-&gt;appendChild($child-&gt;cloneNode(true)); } $spanElement-&gt;parentNode-&gt;replaceChild($p, $spanElement); } // print innerHTML of body element print DOMinnerHTML($document-&gt;getElementsByTagName('body')-&gt;item(0)); // -------------------------------- // Utility function to get innerHTML of an element // -&gt; "stolen" from: http://www.php.net/manual/de/book.dom.php#89718 function DOMinnerHTML($element) { $innerHTML = ""; $children = $element-&gt;childNodes; foreach ($children as $child) { $tmp_dom = new DOMDocument(); $tmp_dom-&gt;appendChild($tmp_dom-&gt;importNode($child, true)); $innerHTML.=trim($tmp_dom-&gt;saveHTML()); } return $innerHTML; } </code></pre> <p>Maybe this can get you in the right direction on how to do the replacement in phpQuery?</p> <hr /> <p>EDIT:</p> <p>I gave the jQuery documentation of replaceWith another look, it seems to me, that you have to pass in the whole html fragment which you want to be your new, replaced content.</p> <p>This code snipped worked for me:</p> <pre><code>$event = phpQuery::newDocumentHTML(...); // iterate over the spans foreach($event-&gt;find('span') as $span) { // make $span a phpQuery object, otherwise its just a DOMElement object $span = pq($span); // fetch the innerHTMLL of the span, and replace the span with &lt;p&gt; $span-&gt;replaceWith('&lt;p&gt;' . $span-&gt;html() . '&lt;/p&gt;'); } print (string) $event; </code></pre> <p>I couldnt find any way to do this with chained method calls in one line.</p>
    singulars
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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