Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

How to extract values from html code using php?



6:44 am on Jan 22, 2012 (gmt 0)

5+ Year Member


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>


<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.

$test="html code here";
$doc=new DOMDocument();


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.

Software error:

Can't locate /home/deploy/webmasterworld/code_format-v6.lib in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.18.2 /usr/local/share/perl/5.18.2 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.18 /usr/share/perl/5.18 /usr/local/lib/site_perl .) at decode-post-v6.lib line 27, <THREADDAT> line 3.

For help, please send mail to the webmaster (it@imninjas.com), giving this error message and the time and date of the error.