tomasd Posted May 20, 2007 Share Posted May 20, 2007 Dear all, I need your help with preg_match, I've got a curl_exec htlm output to parse. Only data I need from this output is located in the following string: ...snip... <FONT class=price>239.00 GBP</FONT> ...snip... Thanks very much for your help in advance!!! Quote Link to comment Share on other sites More sharing options...
MadTechie Posted May 20, 2007 Share Posted May 20, 2007 try <?php $data = "<FONT class=price>239.00 GBP</FONT>"; if (preg_match('%<Font\sclass=price>([0-9\.]+\s+\w+)\<\/Font>%i', $data, $regs)) { $price = $regs[0]; } else { $price = ""; } echo $price; ?> Quote Link to comment Share on other sites More sharing options...
tomasd Posted May 20, 2007 Author Share Posted May 20, 2007 Thanks very much for your reply, your solution works fine when $data = "<FONT class=price>239.00 GBP</FONT>"; however my $data is much more than "<FONT class=price>239.00 GBP</FONT>", as a matter of fact I have whole html output for a page, is there any of extracting the price from it at all? cheers! Quote Link to comment Share on other sites More sharing options...
MadTechie Posted May 20, 2007 Share Posted May 20, 2007 It should find the price in a the html if its a whole page or just a shortline can you post a failed input please Quote Link to comment Share on other sites More sharing options...
MadTechie Posted May 20, 2007 Share Posted May 20, 2007 OH if you post a lot please use code tags Quote Link to comment Share on other sites More sharing options...
tomasd Posted May 20, 2007 Author Share Posted May 20, 2007 I'm using following code: $output = curl_exec ($ch); if (preg_match('%<Font\\sclass=price>([0-9\\.]+\\s+\\w+)\\<\\/Font>%i', $output, $regs)) { $price = $regs[0]; } else { $price = "failed\n"; } echo $price; after running the file: [tomas@dummy curl]$ php -f curl.php failed If I simply echo $output and grep for <FONT class=price> I'm getting what I'm supposed to... Quote Link to comment Share on other sites More sharing options...
MadTechie Posted May 20, 2007 Share Posted May 20, 2007 change if (preg_match('%<Font\\sclass=price>([0-9\\.]+\\s+\\w+)\\<\\/Font>%i', $output, $regs)) { to if (preg_match('%<Font\sclass=price>([0-9\.]+\s+\w+)\<\/Font>%i', $output, $regs)) { Quote Link to comment Share on other sites More sharing options...
MadTechie Posted May 20, 2007 Share Posted May 20, 2007 <?php $data = curl_exec ($ch); if (preg_match('%<Font\sclass=price>([0-9\.]+\s+\w+)\<\/Font>%i', $data, $regs)) { $price = $regs[0]; } else { $price = ""; } echo $price; ?> Quote Link to comment Share on other sites More sharing options...
tomasd Posted May 20, 2007 Author Share Posted May 20, 2007 Hi, I still have no luck Here's the full code I'm using: <?php $ch = curl_init(); $today = date("Ymd"); $date = $today + 101; curl_setopt($ch, CURLOPT_URL, "http://www.bookryanair.com/skylights/cgi-bin/skylights.cgi"); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch,CURLOPT_POSTFIELDS,"travel_type=on§or1_o=STN§or1_d=MJV&ADULT=1&nom=1 &date1=$date&date2=''&page=SELECTED"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $data = curl_exec ($ch); if (preg_match('%<Font\sclass=price>([0-9\.]+\s+\w+)\<\/Font>%i', $data, $regs)) { $price = $regs[0]; } else { $price = ""; } echo $price; curl_close ($ch); ?> Quote Link to comment Share on other sites More sharing options...
MadTechie Posted May 20, 2007 Share Posted May 20, 2007 Can you echo the $data, as my server just times out.. post in code tags Quote Link to comment Share on other sites More sharing options...
tomasd Posted May 20, 2007 Author Share Posted May 20, 2007 I ran php -f curl.php > /home/ftp/echo you can find it here: ftp://wally.hoptp.org/echo Quote Link to comment Share on other sites More sharing options...
MadTechie Posted May 20, 2007 Share Posted May 20, 2007 i got The server at wally.hoptp.org is taking too long to respond. Quote Link to comment Share on other sites More sharing options...
tomasd Posted May 20, 2007 Author Share Posted May 20, 2007 sorry my bad, I should have port forwarded to diff address, anyway you can find the file at http://wally.hopto.org/echo.txt cheeers! Quote Link to comment Share on other sites More sharing options...
MadTechie Posted May 20, 2007 Share Posted May 20, 2007 ahh ok you missed the ' heres an example that should help <?php if (preg_match("%<Font\sclass='price'>([0-9\.]+\s+\w+)\<\/Font>%i", $data, $regs)) { $price = $regs[0]; } else { $price = ""; } echo "FIRST: $price<br />"; preg_match_all("%<Font\sclass='price'>([0-9\.]+\s+\w+)\<\/Font>%i", $data, $result, PREG_PATTERN_ORDER); $result = $result[0]; echo "and the rest:<br />"; foreach($result as $price) { echo "$price<br />"; } ?> Quote Link to comment Share on other sites More sharing options...
tomasd Posted May 20, 2007 Author Share Posted May 20, 2007 you are the best! [tomas@dummy tomas]$ php -f curl.php FIRST: <font class='price'>34.99 GBP</font><br />and the rest:<br /><font class='price'>34.99 GBP</font><br /><font class='price'>34.99 GBP</font><br />[tomas@dummy tomas]$ Thanks ever so much for your help and patience!!! p.s. my holidays to spain Shall become more often Quote Link to comment Share on other sites More sharing options...
MadTechie Posted May 20, 2007 Share Posted May 20, 2007 Cool Well happy holidays and happy coding Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.