Page 1 of 1

Content Scraping with regex

Posted: Thu Oct 30, 2008 1:29 am
by ihsaan
Below is the script that i have in place. I would like to extract 4 fields but this one only gets one. All four fields have the same matching text before and after (i.e. <div align..) How would i modify the code maybe to run a loop that run untill $num=4 or untill no more matches is found and then slip out the results?
$data = file_get_contents('http://mytestsite.htm');
$regex = '/<div align="right" class="style13">(.*?)<\/div>/';
preg_match($regex,$data,$match);
var_dump($match);
echo $match[1];

Re: Content Scraping with regex

Posted: Thu Oct 30, 2008 1:30 am
by s.dot

Re: Content Scraping with regex

Posted: Thu Oct 30, 2008 2:12 am
by ihsaan
Thanx alot man. it worked but still one issue.. There are four results and i want them to each be in their own $variable. Here are the current results:
array(2) { [0]=> array(4) { [0]=> string(102) "
R2,356.15
" [1]=> string(58) "
R5,890.37
" [2]=> string(86) "
R117.81
" [3]=> string(47) "
R39.31
" } [1]=> array(4) { [0]=> string(61) "R2,356.15 " [1]=> string(17) "R5,890.37 " [2]=> string(45) "R117.81 " [3]=> string(6) "R39.31" } }

Re: Content Scraping with regex

Posted: Thu Oct 30, 2008 2:34 am
by GeertDD

Code: Select all

$var1 = $matches[0][0];
$var2 = $matches[0][1];
$var3 = ...
Like that? Or maybe you could use a list() construction.