kevin_007 Posted March 7, 2011 Share Posted March 7, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/229891-simple-html-dom/ Share on other sites More sharing options...
DarkMantis Posted March 7, 2011 Share Posted March 7, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/229891-simple-html-dom/#findComment-1184085 Share on other sites More sharing options...
kevin_007 Posted March 9, 2011 Author Share Posted March 9, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/229891-simple-html-dom/#findComment-1185058 Share on other sites More sharing options...
akeane Posted April 9, 2011 Share Posted April 9, 2011 Add an if statement: foreach ($content->find('h2') as $heading) { if ( $heading->plaintext == 'title 1' ) echo $heading; // *** ADD THIS *** } Quote Link to comment https://forums.phpfreaks.com/topic/229891-simple-html-dom/#findComment-1199369 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.