Jump to content

[SOLVED] help with preg_match


tomasd

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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&sector1_o=STN&sector1_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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.