George Irimiciuc
George Irimiciuc

Reputation: 4633

PHP simple HTML DOM parse

I want to extract some information from some html code using dom parser, but I'm stuck at a point.

<div id="posts">
    <div class="post">
        <div class="user">me:</div>
        <div class="post">I am an apple</div>
    </div>
    <div class="post">
        <div class="user">you:</div>
        <div class="post">I am a banana</div>
    </div>
    <div class="post">
        <div class="user">we:</div>
        <div class="post">We are fruits</div>
    </div>
</div>

This will print the users.

$users= $html->find('div[class=user]');
foreach($users as $user)
    echo $user->innertext;

This will print the posts.

$posts = $html->find('div[class=post]');
foreach($posts as $post)
    echo $post->innertext;

I want to print them together, and not sepparately, like so:

me:
I am an apple
you:
I am a banana
we:
We are fruits

How can I do this using the parser?

Upvotes: 2

Views: 424

Answers (3)

Sithu
Sithu

Reputation: 4862

Assuming that you are using Simple HTML DOM Parser, you can use find() with comma separator format. Try this:

$posts = $html->find('div.post');
foreach($posts as $post){
  $children = $post->find('div.user,div.post');
  foreach($children as $child){
    echo $child->class.' -- ';
    echo $child->innerText(); echo '<br>';
  }
}

Output

user -- me:
post -- I am an apple
user -- you:
post -- I am a banana
user -- we:
post -- We are fruits

Upvotes: 1

Kevin
Kevin

Reputation: 41903

Using the markup you provided, you can just point out the children of the main div (div#posts), then loop all children. Then for each children just get the first and second ones:

foreach($html->find('div#posts', 0)->children() as $post) {
    $user = $post->children(0)->innertext;
    $post = $post->children(1)->innertext;
    echo $user . '<br/>' . $post . '<hr/>';
}

Though I would really suggest use DOMDocument with this:

$dom = new DOMDocument;
$dom->loadHTML($html_markup);
$xpath = new DOMXpath($dom);
$elements = $xpath->query('//div[@id="posts"]/div[@class="post"]');
foreach($elements as $posts) {
    $user = $xpath->evaluate('string(./div[@class="user"])', $posts);
    $post = $xpath->evaluate('string(./div[@class="post"])', $posts);
    echo $user . '<br/>' . $post . '<hr/>';
}

Upvotes: 1

Utkarsh Dixit
Utkarsh Dixit

Reputation: 4275

Use the code below

$users= $html->find('div[class=user]');
$posts = $html->find('div[class=post]');
foreach($users as $i=>$user){
    echo $user->innertext."<br>";
echo $posts[$i]->innertext;
    }

Hope this helps you

Upvotes: 0

Related Questions