title: PHP Preg_match Image from url
body: <p>I am trying to parse a website and grab the name or url of the image.</p>
  
  <p>Example URL:
  <a href="http://www.theworkingmanstore.com/georgia-gr14-infants-romeo.aspx" rel="nofollow">http://www.theworkingmanstore.com/georgia-gr14-infants-romeo.aspx</a></p>
  
  <p>There are 6 images or more in a single <code>&lt;td&gt;</code> and I only want to get the first img src in that <code>&lt;td&gt;</code>. </p>
  
  <p>I am sure it can probably be done with Dom Parser, but I have no experience with it. </p>
  
  <p>Any assistance would be appreciated.</p>
  
  <p>Thanks</p>
  
  <pre><code>$html = file_get_contents($url);
  $reg = '/img src=["\']?([^"\' ]*)["\' ]/';
  preg_match_all($reg, $html, $m);
  $arr = array_map(function($v){
  return trim(str_replace(array('img src=', 'http://www.theworkingmanstore.com'), '', $v), '"');}, $m[0]);
  print_r($arr)
  </code></pre>
  
  <p>Output:
  This is output from regex</p>
  
  <pre>Array
  (
      [0] =&gt; /images/logo2.png
      [1] =&gt; /images/mod_head_category_lt.gif
      [2] =&gt; '/images/products/display/GR14_EXTRALARGE.jpg'
      [3] =&gt; '/images/products/thumb/GR14_EXTRALARGE.jpg'
      [4] =&gt; '/images/products/thumb/GR14_8_EXTRALARGE.jpg'
      [5] =&gt; '/images/products/thumb/GR14_5_EXTRALARGE.jpg'
      [6] =&gt; '/images/products/thumb/GR14_3_EXTRALARGE.jpg'
      [7] =&gt; '/images/products/thumb/GR14_42_EXTRALARGE.jpg'
      [8] =&gt; '/images/products/thumb/GR14_2_EXTRALARGE.jpg'
      [9] =&gt; /images/freeshipping.jpg
      [10] =&gt; /images/facebook_32.png
      [11] =&gt; images/twitter_32.png
      [12] =&gt; images/googleplus_32.png
      [13] =&gt; images/pinterest_32.png
      [14] =&gt; /images/payments.gif
      [15] =&gt; /images/brands/the-working-man.jpg
  )
  </pre> 
  
  <p>Tried the Dom Parser suggestion:</p>
  
  <pre><code>$html = file_get_contents($url) ;
  $dom = new DOMDocument();
  $dom-&gt;loadHtml($html);    
  $xpath = new DOMXPath($dom);
  echo $xpath-&gt;evaluate(
  'string(//td/a[@id = "Zoomer"]/descendant::img[1]/@src)'
  );
  </code></pre>
  
  <p>Got Error as output:
  Warning: DOMDocument::loadHTML() [domdocument.loadhtml]: Tag nav invalid in Entity</p>