francisMi francisMi -4 years ago 156
PHP Question

PHP web scraping

I use php web scraping, and I want to get the price (3.65) on Sunday form the html code below:

<tr class="odd">
<td >
<b>Sunday</b> Info
<div class="test">test</div>
</td>
<td>
&euro; 3.65 *

</td>
</tr>


But I don't find the best regex to do this...
I use this php code:

<?php
$data = file_get_contents('http://www.test.com/');

preg_match('/<tr class="odd"><td ><b>Sunday</b> Info<div class="test">test<\/div><\/td><td>&euro; (.*) *<\/td><\/tr>/i', $data, $matches);
$result = $matches[1];
?>


But no result... What's wrong in the regex? (I think it's because of the new lines/spaces?)

Answer Source

The problems are the spaces between the tags. there a line breaks, tabs and/or spaces.

your regex doesn't match to them.

you also need to setup your preg_match for multiline!

i think it is more easy to use xpath for scraping.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download