Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Jul 2004
    Posts
    201
    Thanks
    8
    Thanked 1 Time in 1 Post

    Quick Regular Expressions Question

    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 Code:
    <?php

    $ch 
    curl_init();
    curl_setopt($chCURLOPT_URL'http://maps.google.co.uk/maps?f=d&hl=en&saddr=m3&daddr=m2');
    curl_setopt($chCURLOPT_HEADER1);
    curl_setopt($chCURLOPT_RETURNTRANSFER1);
    $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:

    PHP Code:
    /timedist ul042>(.*)/
    Cant get the number. In the source file the code is:

    PHP Code:
    timedist ul042>0.7 
    I simply want the scrip to get the number 0.7 from there lol.

    thanks for your time.

  • #2
    New Coder
    Join Date
    Mar 2005
    Location
    Indiana (USA)
    Posts
    50
    Thanks
    4
    Thanked 0 Times in 0 Posts
    I am HORRIBLE with regular expressions, but you could use the explode function. It could be something like this:

    PHP Code:
    $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

    PHP Code:
    $info[count($info) - 1

  • #3
    Regular Coder
    Join Date
    Jul 2004
    Posts
    201
    Thanks
    8
    Thanked 1 Time in 1 Post
    hmmm, didnt think of that. Anyways, I tried this:

    PHP Code:
    $ch curl_init();
    curl_setopt($chCURLOPT_URL'http://maps.google.co.uk/maps?f=d&hl=en&saddr=m3&daddr=m2');
    curl_setopt($chCURLOPT_HEADER1);
    curl_setopt($chCURLOPT_RETURNTRANSFER1);
    $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.

  • #4
    New Coder
    Join Date
    Mar 2005
    Location
    Indiana (USA)
    Posts
    50
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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.

  • #5
    Regular Coder
    Join Date
    Jul 2004
    Posts
    201
    Thanks
    8
    Thanked 1 Time in 1 Post
    Ok, thanks for your time anyways, hoping somebody else can help me fix the expression.

  • #6
    Regular Coder
    Join Date
    Feb 2007
    Location
    near Washington, DC
    Posts
    135
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:
    Code:
    /timedist ul042>([0-9\.]*)/
    I still struggle with regex's sometimes. I'm getting a lot better with them, but note my sig.
    Last edited by phoenixshade; 03-11-2007 at 04:14 AM. Reason: Misread last char in user's code as '\s' ... removed reference to space
    — Wilford Nusser
    Validate Your Code: (X)HTML CSS
    An HTML Email is NOT a Web Page: HTML Email Guide (1.2Mb pdf) Webmail CSS Support
    REGEX: Brought to you by Psychotic Crack-Smoking Monkeys

  • #7
    Regular Coder
    Join Date
    Jul 2004
    Posts
    201
    Thanks
    8
    Thanked 1 Time in 1 Post
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •