Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP remove all div with class="myclass" except first one + add another div instead of others
    text
    copied!<p>I have a $content with</p> <pre><code>&lt;div class="slidesWrap"&gt; &lt;div class="slidesСontainer"&gt; &lt;div class="myclass"&gt; ... &lt;/div&gt; &lt;div class="myclass"&gt; ... &lt;/div&gt; ... ... &lt;div class="myclass"&gt; ... &lt;/div&gt; &lt;/div&gt; &lt;div class="nav"&gt; ... &lt;/div&gt; &lt;/div&gt; some other text here, &lt;p&gt;&lt;/p&gt; bla-bla-bla </code></pre> <p>I would like to remove via PHP all the divs with class="myclass" except the first one, and add another div instead of others, so that the result is:</p> <pre><code>&lt;div class="slidesWrap"&gt; &lt;div class="slidesСontainer"&gt; &lt;div class="myclass"&gt; ... &lt;/div&gt; &lt;div&gt;Check all divs &lt;a href="myurl"&gt;here&lt;/a&gt;&lt;/div&gt; &lt;/div&gt; &lt;div class="nav"&gt; ... &lt;/div&gt; &lt;/div&gt; some other text here, &lt;p&gt;&lt;/p&gt; bla-bla-bla </code></pre> <p>Would be grateful if someone can point me a solution.</p> <p>UDATE2: </p> <p>some similar question <a href="https://stackoverflow.com/questions/6438035/remove-all-list-elements-except-the-first-in-php">here</a> from that I came up with the following test code:</p> <pre><code>$content = '&lt;div class="slidesWrap"&gt; &lt;div class="slidesСontainer"&gt; &lt;div class="myclass"&gt; &lt;/div&gt; &lt;div class="myclass"&gt; &lt;/div&gt; &lt;div class="myclass"&gt; &lt;/div&gt; &lt;/div&gt; &lt;div class="nav"&gt; &lt;/div&gt; &lt;/div&gt; some other text here, &lt;p&gt;&lt;/p&gt; bla-bla-bla'; $dom = new DOMDocument(); $dom-&gt;loadHtml($content); $xpath = new DOMXPath($dom); foreach ($xpath-&gt;query('//*[@class="myClass" and position()&gt;1]') as $liNode) { $liNode-&gt;parentNode-&gt;removeChild($liNode); } echo $dom-&gt;saveXml($dom-&gt;documentElement); </code></pre> <p>Any ideas where I can test it?</p>
 

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