title: Scraping search results using PHP and Simple HTML DOM
body: <p>I'm working on making a web scraper where the goal is to allow a user to enter a search term, click a submit button, and receive a list of items related to their search. </p>
  
  <p>Basically like a search engine would do, but instead of Google, I want the search to start from here: <a href="https://digitalcommons.colby.edu/special_collections/" rel="nofollow noreferrer">https://digitalcommons.colby.edu/special_collections/</a></p>
  
  <p>The script below mostly works, but I'd like to have <code>$searchString</code> come from an input field on a form and then return the list of results after clicking the submit button. I'm currently using simple_html_dom.php.</p>
  
  <p>Any help or pointers would be greatly appreciated.</p>
  
  <pre><code>&lt;?php 
  include_once('assets/inc/simple_html_dom.php');
  
  $searchString = "Significant Inventions";
  $in = $searchString;
  $in = str_replace(' ','+',$in); // space is a +
  $url  = 'http://www.google.com/search?hl=en&amp;tbo=d&amp;site=&amp;source=hp&amp;q='.$in.'&amp;oq='.$in.'';
  
  echo '&lt;h2&gt;Title: ' . $searchString . '&lt;/h2&gt;';
  echo '&lt;span&gt;Title: ' . $url . '&lt;span&gt;&lt;hr&gt;';
  
  $html = file_get_html($url);
  
  $i=0;
  $linkObjs = $html-&gt;find('h3.r a'); 
  foreach ($linkObjs as $linkObj) {
      $title = trim($linkObj-&gt;plaintext);
      $link  = trim($linkObj-&gt;href);
  
      // if it is not a direct link but url reference found inside it, then extract
      if (!preg_match('/^https?/', $link) &amp;&amp; preg_match('/q=(.+)&amp;amp;sa=/U', $link, $matches) &amp;&amp; preg_match('/^https?/', $matches[1])) {
          $link = $matches[1];
      } else if (!preg_match('/^https?/', $link)) { // skip if it is not a valid link
          continue;
      }
  
      $descr = $html-&gt;find('span.st',$i); // description is not a child element of H3 thereforce we use a counter and recheck.
      $i++;   
      echo '&lt;p&gt;Title: ' . $title . '&lt;br /&gt;';
      echo 'Link: &lt;a href="' . $link . '" target="_blank"&gt;Read More&lt;/a&gt;&lt;br /&gt;';
      echo 'Description: ' . $descr . '&lt;/p&gt;&lt;hr&gt;';
  }
  ?&gt;
  </code></pre>
  
  <p>The above script is modified from <a href="https://stackoverflow.com/questions/26238146/scraping-google-front-page-results-with-php">Scraping Google Front Page Results with php</a>.</p>