How to pull some variables from a string

Discussion in 'PHP' started by mickmel, Apr 16, 2008.

  1. #1
    Suppose someone submits this URL to my site:

    http://maps.google.com/?ie=UTF8&ll=39.143774,-94.577351&spn=0.0448,0.080338&z=14
    &layer=c&cbll=39.12826,-94.577457&cbp=1,293.8834236345768,,0,1.979445749803011

    And then that whole string is stored in "$url_submitted"

    How can I pull out some of those individual numbers to store?

    For example, I want the two numbers in the "cbll" to be stored separately (say in $a and $b), and I want the items in "cbp" to be stored separately (say in $c, $d, $e and $f).

    How would I go about doing this?

    Thanks!
     
    mickmel, Apr 16, 2008 IP
  2. Louis11

    Louis11 Active Member

    Messages:
    783
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    70
    #2
    Look into the preg_match and preg_match_all functions, and familiarize yourself with Regular Expressions (called 'regex' for short).

    Just a quick little example (might run, might not . . . doing this straight off the top of my head. Correct any errors for yourself ;))

    
     <?
     // get the cbll
     preg_match("/cbll=.*&cbp/", $url_submitted, $matches);
     print_r($matches); // print the matches, do whatever you want with them
    
    // you could make the regex above better, but i'm tired and have been 
    // coding for 5 hours straight so, again i'll leave this to you and simply 
    // str_remove the excess baggage
    $remove = array("cbll=", "&cbp");
    $cbll = str_replace($remove, "", $matches[0]);
    
     ?>
    
    PHP:
    Hope that helps :)
     
    Louis11, Apr 16, 2008 IP
  3. mickmel

    mickmel Peon

    Messages:
    384
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for the feedback. I'll dig into it.
     
    mickmel, Apr 16, 2008 IP
  4. Louis11

    Louis11 Active Member

    Messages:
    783
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    70
    #4
    No problemo :p Just post back if you need any help!
     
    Louis11, Apr 16, 2008 IP