Got a response. The whole example is a bit long, but it works. I also show the result.
HTML for what we will look:
<html> <head> <title>Simple HTML DOM - Find Text</title> </head> <body> <h3>Simple HTML DOM - Find Text</h3> <div id="first"> <p>This is a paragraph inside of div 'first'. This paragraph does not have the text we are looking for.</p> <p>As a matter of fact this div does not have the text we are looking for</p> </div> <div id="second"> <ul> <li>This is an unordered list. <li id="love1">We are looking for the following word love. <li>Does not contain the word. </ul> <p id="love2">This paragraph which is in div second contains the word love.</p> </div> <div id="third"> <a id="love3" href="goes.nowhere.com">link to love site</a> </div> </body> </html>
PHP:
<?php include_once('simple_html_dom.php'); function scraping_for_text($iUrl,$iText) { echo "iUrl=".$iUrl."<br />"; echo "iText=".$iText."<br />"; // create HTML DOM $html = file_get_html($iUrl); // get text elements $aObj = $html->find('text'); if (count($aObj) > 0) { echo "<h4>Found ".$iText."</h4>"; } else { echo "<h4>No ".$iText." found"."</h4>"; } foreach ($aObj as $key=>$oLove) { $plaintext = $oLove->plaintext; if (strpos($plaintext,$iText) !== FALSE) { echo $key.": text=".$plaintext."<br />" ."--- parent tag=".$oLove->parent()->tag."<br />" ."--- parent id=".$oLove->parent()->id."<br />"; } } // clean up memory $html->clear(); unset($html); return; } // ------------------------------------------------------------- // test it! // user_agent header... ini_set('user_agent', 'My-Application/2.5'); scraping_for_text("test_text.htm","love"); ?>
Exit:
iUrl=test_text.htm iText=love Found love 18: text=We are looking for the following word love. --- parent tag=li --- parent id=love1 21: text=This paragraph which is in div second contains the word love. --- parent tag=p --- parent id=love2 25: text=link to love site --- parent tag=a --- parent id=love3
What they all wrote !!!!
akeane
source share