Nick
Nick

Reputation: 9373

preg_match to get div contents

I am trying to get the contents of a div named: <img id="hplogo-img" src="thelinkiwant"/>

I have this code which isn't working, it just echo's 'Array':

<?php 
include_once('simple_html_dom.php');
$html = file_get_html($url); 
preg_match('/<img id= \'hplogo-img\'>(.*)<\/div>/s',$html,$matches);
echo $matches;
?>

If it's possible to do this with straight PHP that would be preferred. Any idea's why I can't get the link from the div?

Upvotes: 0

Views: 1561

Answers (2)

conradkleinespel
conradkleinespel

Reputation: 6987

$matches is an array.

Try using

print_r($matches)

You should see the arrays content :)

The first element should be what you're looking for. So make:

echo $matches[0];

Upvotes: 2

xdazz
xdazz

Reputation: 160833

Why not use the method the parser provide.

$ret = $html->find('img[id=hplogo-img]'); 

Upvotes: 2

Related Questions