homepage Welcome to WebmasterWorld Guest from 54.196.62.23
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
How to extract values from html code using php?
Thaparian

5+ Year Member



 
Msg#: 4409566 posted 6:44 am on Jan 22, 2012 (gmt 0)

Hi,

I need to extract link url and image url from a peace of html code.

Sample HTML code

<a href="link to full size image"><img src="medium size image path here"/></a>

Or

<img src="medium size image path here" alt ="" />


If a link is present, I need the full size image url, otherwise I need the medium size image path.


This is the code that I made, but I think this is not the right way to get this done.

<?php
$test="html code here";
$doc=new DOMDocument();
$doc->loadHTML("$test");
$xml=simplexml_import_dom($doc);

$images=$xml->xpath('//img');

foreach ($images as $img) {

$thumburl = $img['src'];
}

$href = preg_match('/\shref="(?<href>[^"]+)"/', $test, $match);


if ($href!="") {
$href = $match[1];
echo $href;
}
else {
echo $thumburl;
}
?>


Please help.

 

brotherhood of LAN

WebmasterWorld Administrator brotherhood_of_lan us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



 
Msg#: 4409566 posted 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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved