Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7009377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:51:15+00:00 2026-05-27T21:51:15+00:00

I have a $content with <div class=slidesWrap> <div class=slidesСontainer> <div class=myclass> … </div> <div

  • 0

I have a $content with

<div class="slidesWrap">
   <div class="slidesСontainer">
      <div class="myclass"> 
        ...
      </div>
      <div class="myclass">
        ...
      </div>
      ...
      ...
      <div class="myclass">
        ...
      </div>
   </div>
   <div class="nav">
      ...
   </div>
</div>
some other text here, <p></p> bla-bla-bla

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:

<div class="slidesWrap">
   <div class="slidesСontainer">
      <div class="myclass"> 
        ...
      </div>
      <div>Check all divs <a href="myurl">here</a></div>
   </div>
   <div class="nav">
      ...
   </div>
</div>
some other text here, <p></p> bla-bla-bla

Would be grateful if someone can point me a solution.

UDATE2:

some similar question here
from that I came up with the following test code:

$content = '<div class="slidesWrap">
   <div class="slidesСontainer">
      <div class="myclass">
      </div>
      <div class="myclass">
      </div>
      <div class="myclass">
      </div>
   </div>
   <div class="nav">
  </div>
  </div>
 some other text here, <p></p> bla-bla-bla';

$dom = new DOMDocument();
$dom->loadHtml($content);

$xpath = new DOMXPath($dom);
foreach ($xpath->query('//*[@class="myClass" and position()>1]') as $liNode) {
    $liNode->parentNode->removeChild($liNode); 
}

echo $dom->saveXml($dom->documentElement);

Any ideas where I can test it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T21:51:16+00:00Added an answer on May 27, 2026 at 9:51 pm

    Here is what you are looking for (similar to your edit, but it removes the added html tags):

    $doc = new DOMDocument();
    $doc->loadHTML($content);
    
    $xp = new DOMXpath($doc);
    $elements = $xp->query("//div[@class='myclass']");
    
    if($elements->length > 1)
    {
        $newElem = $doc->createElement("div");
        $newElem->appendChild($doc->createTextNode("Check all divs "));
        $newElemLink = $newElem->appendChild($doc->createElement("a"));
        $newElemLink->setAttribute("href", "myurl");
        $newElemLink->appendChild($doc->createTextNode("here"));
    
        $elements->item(1)->parentNode->replaceChild($newElem, $elements->item(1));     
    
        for($i = $elements->length - 1; $i > 1 ; $i--)
        {
            $elements->item($i)->parentNode->removeChild($elements->item($i));
        }
    }
    
    echo $doc->saveXML($doc->getElementsByTagName('div')->item(0));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

can we have content text directly in <div>text content</div> or it should be like
I have this code: <div class = content-dir-item> <p>Text input</p> <img src=./images/email.png class =
I have a series of divs: <div class=some_class active>Some Content</div> <div class=some_class>Different Content</div> <div
I have dynamic content in a div called txtresults like this: <div id=txtresults></div> This
If I have HTML content in a variable like so: var data = <div
I have the below xpath expression //div[@class=post-content]//img which runs on a html page, scanning
I have a page similar to: <div id=content> <div id=boxes> <div id=box:content:1:text /> <div
I want to parse a html content that have something like this: <div id=sometext>Lorem<br>
I have a page setup as follows <div class="Header"> Header content </div> <div class="Content">
<div class=boxcontent> <div>some content this div may be missing [dynamic genrated]</div> <div class=elem></div> <div

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.