Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Got the answer. The entire example is a little long but it works. I also show the output.</p> <p>The HTML for what we are going to look at:</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;title&gt;Simple HTML DOM - Find Text&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;h3&gt;Simple HTML DOM - Find Text&lt;/h3&gt; &lt;div id="first"&gt; &lt;p&gt;This is a paragraph inside of div 'first'. This paragraph does not have the text we are looking for.&lt;/p&gt; &lt;p&gt;As a matter of fact this div does not have the text we are looking for&lt;/p&gt; &lt;/div&gt; &lt;div id="second"&gt; &lt;ul&gt; &lt;li&gt;This is an unordered list. &lt;li id="love1"&gt;We are looking for the following word love. &lt;li&gt;Does not contain the word. &lt;/ul&gt; &lt;p id="love2"&gt;This paragraph which is in div second contains the word love.&lt;/p&gt; &lt;/div&gt; &lt;div id="third"&gt; &lt;a id="love3" href="goes.nowhere.com"&gt;link to love site&lt;/a&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>The PHP:</p> <pre><code>&lt;?php include_once('simple_html_dom.php'); function scraping_for_text($iUrl,$iText) { echo "iUrl=".$iUrl."&lt;br /&gt;"; echo "iText=".$iText."&lt;br /&gt;"; // create HTML DOM $html = file_get_html($iUrl); // get text elements $aObj = $html-&gt;find('text'); if (count($aObj) &gt; 0) { echo "&lt;h4&gt;Found ".$iText."&lt;/h4&gt;"; } else { echo "&lt;h4&gt;No ".$iText." found"."&lt;/h4&gt;"; } foreach ($aObj as $key=&gt;$oLove) { $plaintext = $oLove-&gt;plaintext; if (strpos($plaintext,$iText) !== FALSE) { echo $key.": text=".$plaintext."&lt;br /&gt;" ."--- parent tag=".$oLove-&gt;parent()-&gt;tag."&lt;br /&gt;" ."--- parent id=".$oLove-&gt;parent()-&gt;id."&lt;br /&gt;"; } } // clean up memory $html-&gt;clear(); unset($html); return; } // ------------------------------------------------------------- // test it! // user_agent header... ini_set('user_agent', 'My-Application/2.5'); scraping_for_text("test_text.htm","love"); ?&gt; </code></pre> <p>The output:</p> <pre><code>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 </code></pre> <p>That's all they wrote!!!!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload