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!!! Link to comment https://forums.phpfreaks.com/topic/52220-solved-help-with-preg_match/ 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; ?> Link to comment https://forums.phpfreaks.com/topic/52220-solved-help-with-preg_match/#findComment-257565 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! Link to comment https://forums.phpfreaks.com/topic/52220-solved-help-with-preg_match/#findComment-257567 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 Link to comment https://forums.phpfreaks.com/topic/52220-solved-help-with-preg_match/#findComment-257573 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 Link to comment https://forums.phpfreaks.com/topic/52220-solved-help-with-preg_match/#findComment-257578 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... Link to comment https://forums.phpfreaks.com/topic/52220-solved-help-with-preg_match/#findComment-257579 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)) { Link to comment https://forums.phpfreaks.com/topic/52220-solved-help-with-preg_match/#findComment-257580 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; ?> Link to comment https://forums.phpfreaks.com/topic/52220-solved-help-with-preg_match/#findComment-257584 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); ?> Link to comment https://forums.phpfreaks.com/topic/52220-solved-help-with-preg_match/#findComment-257590 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 Link to comment https://forums.phpfreaks.com/topic/52220-solved-help-with-preg_match/#findComment-257597 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 Link to comment https://forums.phpfreaks.com/topic/52220-solved-help-with-preg_match/#findComment-257606 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. Link to comment https://forums.phpfreaks.com/topic/52220-solved-help-with-preg_match/#findComment-257608 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! Link to comment https://forums.phpfreaks.com/topic/52220-solved-help-with-preg_match/#findComment-257645 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 />"; } ?> Link to comment https://forums.phpfreaks.com/topic/52220-solved-help-with-preg_match/#findComment-257666 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 Link to comment https://forums.phpfreaks.com/topic/52220-solved-help-with-preg_match/#findComment-257687 Share on other sites More sharing options...
MadTechie Posted May 20, 2007 Share Posted May 20, 2007 Cool Well happy holidays and happy coding Link to comment https://forums.phpfreaks.com/topic/52220-solved-help-with-preg_match/#findComment-257697 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.