Jump to content

simple html dom


kevin_007

Recommended Posts

suppose a page has the following tags

<div class = "news">
<div class = "article">
<h2>title 1</h2>
<div class = "content">
<p>content 1....</p>
</div>
</div>
<div class = "article">
<h2>title 2</h2>
<div class = "content">
<p>content 2....</p>
</div>
</div>
</div>

is it possible to check, using simple html dom library, whether the value of <h2> is title 1 and then if it is echo it, or store it in a variable?

what i've been able to do up to now is:

<?php

include('simple_html_dom.php');

foreach($article->find('div[class=news]') as $news)

{

foreach ($news->find('div[class=article]') as $content)

{

foreach ($content->find('h2') as $heading)

{

echo $heading;

 

}

}

}

?>

 

this only echos all the h2

Link to comment
Share on other sites

I would personally suggest trying to do most of this using jQuery or simular. You could do:

 

$.(document).ready(function(){
    $.each("h2").html("Title 1", function(){
        $(this).html("Replaced Text");
    });
});

 

However, I'm not overly good with jQuery so you may want to edit that to make it work properly. :)

 

Hope this helps.

 

DarkMantis

Link to comment
Share on other sites

I would personally suggest trying to do most of this using jQuery or simular. You could do:

 

$.(document).ready(function(){
    $.each("h2").html("Title 1", function(){
        $(this).html("Replaced Text");
    });
});

 

However, I'm not overly good with jQuery so you may want to edit that to make it work properly. :)

 

Hope this helps.

 

DarkMantis

 

I don't want to use Jquery because the page is an online page which i find it easier to work with simple html dom library and i don't know JQuery too much. Is it possible to check the value of h2 using simple html dom library?

Link to comment
Share on other sites

  • 1 month later...

Add an if statement:

 

      foreach ($content->find('h2') as $heading)

      {

        if ( $heading->plaintext == 'title 1' ) echo $heading; // *** ADD THIS ***

      }     

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.