title: How to get img src with xpath and php
body: <p>I'm getting an empty variable when I use the following xpath formula:</p>
  
  <pre><code>foreach ($xpath4-&gt;evaluate('/rss/channel/item') as $rssItem) {
  
      $item_image = $xpath4-&gt;evaluate('string(content:encoded/img/@src)', $rssItem);
  
  }
  </code></pre>
  
  <p>Here's the xml portion
  :</p>
  
  <pre><code>&lt;content:encoded&gt;
              &lt;![CDATA[&lt;div id="attachment_195278" style="width: 4082px" class="wp-caption aligncenter"&gt;&lt;img class="size-full wp-image-195278" src="http://website.com/files/2017/03/AP17067056441431.jpg"...
  </code></pre>
  
  <p>How do I get the img src?  I tried experimenting with different syntax/formulas, but they all return an empty variable.  I also can't find any examples of xpath in a for each loop targeting an img src within a content encoded tag.</p>
  
  <p>Edit: I attempted to parse the CDATA with PHP Simple HTML DOM Parser, however, I kept getting the error, "PHP Fatal error:  Call to a member function find() on a non-object."  So I treated the CDATA as a string and parsed it with a function I found:</p>
  
  <pre><code>function getBetween($content,$start,$end){
    $r = explode($start, $content);
    if (isset($r[1])){
      $r = explode($end, $r[1]);
      return $r[0];
  }
    return '';
  }
  
  foreach ($xpath4-&gt;evaluate('/rss/channel/item') as $rssItem) {
  
    $item_image = $xpath4-&gt;evaluate('string(content:encoded)', $rssItem);
  
    $content = $item_image;
    $start = 'src="';
    $end = '" alt=';
    $item_image = getBetween($content,$start,$end);
  
    echo $item_image;
  
  }
  </code></pre>