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 7548019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:36:14+00:00 2026-05-30T09:36:14+00:00

I am trying to find child nodes by a certain class name (divs with

  • 0

I am trying to find child nodes by a certain class name (divs with class name=’foo’) within a loop of DOMDocument nodes. If it exists it should set my foo value to 1:

My HTML $document looks like:

...
<div class="posts">Div Posts 1</div>
<div class="posts">Div Posts 2<div class="foo"></div></div>
<div class="posts">Div Posts 3</div>
<div class="posts">Div Posts 4<div class="foo"></div></div>
<div class="posts">Div Posts 5</div>
...

DOMDocument/Xpath ($document):

$html = array();
$document = new \DOMDocument();
$document->loadHTMLFile($url); // loads html from above
$xpath = new \DOMXPath($document);

$i=0;
foreach ($xpath->query(Parser::cssToXpath('.posts')) as $node) {
    $html['posts'][$i]['content'] = $node->nodeValue;  
    // check if child node with class name 'foo' exists => doesn't work :(
    $children = $node->getElementsByTagName('foo');
    if($children)
        $html['posts'][$i]['foo'] = '1';
    else
        $html['posts'][$i]['foo'] = '0';
    $i++;
}

Output:

[posts] => Array
    (
        [0] => Array
            (
                [content] => Div class Posts 1
                [foo] => 1
            )

        [1] => Array
            (
                [content] => Div class Posts 2
                [foo] => 1
            )

        [2] => Array
            (
                [content] => Div class Posts 3
                [foo] => 1
            )

        [3] => Array
            (
                [content] => Div class Posts 4
                [foo] => 1
            )

        [4] => Array
            (
                [content] => Div class Posts 5
                [foo] => 1
            )

    )

getElementsByTagName() might not be the right method for that, but I tried different methods already and don’t find the right one. 🙁

  • 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-30T09:36:16+00:00Added an answer on May 30, 2026 at 9:36 am

    According to your comment

    Hm yes but still doesn’t work unfortunately. Eventually I need to know which .posts div has the child element ‘foo’ because I need to analyze the content of that parent and also need to replace it later

    to the previous answer your predicate is probably:

    a) select div elements
    b) with attribute class=posts
    c) and with a child element div
    d) which has attribute class=foo

    as xpath expression:

    a) //div
    b) //div[ @class=”posts” ]
    c) //div[ @class=”posts” and div ]
    d) //div[ @class=”posts” and div[ @class=”foo” ] ]

    e.g.

    <?php
    $doc = new DOMDocument;
    $doc->loadhtml( getData() );
    $xpath = new DOMXPath($doc);   
    
    /*
    use something like
        //div[contains(concat(' ',normalize-space(@class),' '),' post ')]
    if the html element may have class="post lalala"
    */
    foreach( $xpath->query('//div[@class="posts" and div[@class="foo"]]') as $post) {
        while ( $post->firstChild ) {
            $post->removeChild( $post->firstChild );
        }   
        $post->appendChild( $doc->createElement('span', 'The quick fox....') );
    }
    echo $doc->savehtml();
    
    
    function getData() {
        return <<< eoh
    <html><head><title>...</title></head><body>
        <div class="posts">Div Posts 1</div>
        <div class="posts">Div Posts 2<div class="foo"></div></div>
        <div class="posts">Div Posts 3</div>
        <div class="posts">Div Posts 4<div class="foo"></div></div>
        <div class="posts">Div Posts 5</div>
    </body></html>
    eoh;
    }
    

    prints

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
    <html><head><title>...</title></head><body>
        <div class="posts">Div Posts 1</div>
        <div class="posts"><span>The quick fox....</span></div>
        <div class="posts">Div Posts 3</div>
        <div class="posts"><span>The quick fox....</span></div>
        <div class="posts">Div Posts 5</div>
    </body></html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to find a way to get the total number of child nodes
I am trying to find a child element within a table row. The code
I'm trying to use Maven2 but my child projects cannot find the parent project.
Trying to find a good name for a method swapping each coordinate X and
I'm trying to use XElement to find out the parent node of a child.
I'm trying to find an elegant way of encapsulating XML into a C# class.
I have this XSLT stylesheet, where I'm trying to find nodes that contain an
I'm trying to find all records in database that have exactly the same set
I'm trying to find if a certain element is present in a XML file
I'm just trying to exclude a couple of child elements by class from a

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.