...

View Full Version : Quick Regular Expressions Question



Anthony2oo4
03-10-2007, 11:02 PM
Im trying to learn regular expressions, and as an example im trying to get the miles from the google maps page. Here is the code I have:


<?php

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://maps.google.co.uk/maps?f=d&hl=en&saddr=m3&daddr=m2');
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
preg_match("/timedist ul\042>(.*)/s",$data,$newresults);
echo "<textarea rows=\"24\" name=\"f\" cols=\"119\">".$data."</textarea>";

print_r ($newresults);

?>

But seems to return a blank array. Can anybody explain wht:


/timedist ul\042>(.*)/s

Cant get the number. In the source file the code is:


timedist ul\042>0.7

I simply want the scrip to get the number 0.7 from there lol.

thanks for your time.

chiefbutz
03-11-2007, 12:22 AM
I am HORRIBLE with regular expressions, but you could use the explode (http://us2.php.net/manual/en/function.explode.php) function. It could be something like this:


$info = explode('>', $data)

If that is the only '>' symbol in the data what you want should be the last element in the array, which you can get by doing the following


$info[count($info) - 1]

Anthony2oo4
03-11-2007, 01:29 AM
hmmm, didnt think of that. Anyways, I tried this:


$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://maps.google.co.uk/maps?f=d&hl=en&saddr=m3&daddr=m2');
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
$newresults = explode('timedist ul042>', $data);
//preg_match("/timedist ul\042>(.*)/s",$data,$newresults);
echo "<textarea rows=\"24\" name=\"f\" cols=\"119\">".$data."</textarea>";

print_r ($newresults);

and that didnt seem to work lol, check out the result here: http://mux-host.com/google.php

If im doing it wrong please correct me.

thanks for your help and time.

chiefbutz
03-11-2007, 01:36 AM
Oh, I think I just understood what you are trying to do. I don't know that my solution will do you any good, I suggest going back to your original code and keep going with the regular expressions.

Anthony2oo4
03-11-2007, 03:02 AM
Ok, thanks for your time anyways, hoping somebody else can help me fix the expression.

phoenixshade
03-11-2007, 05:09 AM
I can't figure out why you are escaping the zero. As far as I know, there's nothing special about this character.

Try this as your regex instead:

/timedist ul042>([0-9\.]*)/
I still struggle with regex's sometimes. I'm getting a lot better with them, but note my sig. :D

Anthony2oo4
03-11-2007, 01:45 PM
Just tried that new code and got a blank array, see here:

http://mux-host.com/google.php

Thanks for your time, I hope someone can get this :(



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum