Page is a not externally linkable
- Code, Content, and Presentation
-- PHP Server Side Scripting
---- How to extract values from html code using php?


brotherhood_of_LAN - 7:13 am on Jan 22, 2012 (gmt 0)


It looks like you can avoid using regex in this example. Try this code.


<?php
$test="<a href=\"/path/to/somewhere\">test</a>
<a href=\"/path/to/blah\"><img src=\"/path/to/image\" /></a>";
$doc=new DOMDocument();
$doc->loadHTML($test);

// Get all <a> tags
$a = $doc->getElementsByTagName('a');
// Count of <a> tags
$alen = $a->length;
      for($i = 0;$i < $alen;$i++)
      {
      // return href if it exists into $href
            if($a->item($i)->hasAttribute('href'))
                  $href = $a->item($i)->getAttribute('href');
      /*
      return src attribute of image into $src if 
            1) parent element is <a> 
            2) element is <img>
            3) src attribute exists
      */ 
            if($a->item($i)->haschildNodes() &;&; $a->item($i)->firstChild->nodeName == 'img' &;&; $a->item($i)->firstChild->hasAttribute('src'))
                  $src = $a->item($i)->firstChild->getAttribute('src');
      }

?> 

You'll need to replace the &;&; with && as the syntax highlighter has altered it.


Thread source:: http://www.webmasterworld.com/php/4409566.htm
Brought to you by WebmasterWorld: http://www.webmasterworld.com