Content Scraping with regex

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
ihsaan
Forum Newbie
Posts: 2
Joined: Thu Oct 30, 2008 1:22 am

Content Scraping with regex

Post 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];
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Content Scraping with regex

Post by s.dot »

Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
ihsaan
Forum Newbie
Posts: 2
Joined: Thu Oct 30, 2008 1:22 am

Re: Content Scraping with regex

Post 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" } }
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Re: Content Scraping with regex

Post by GeertDD »

Code: Select all

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