Stripping variables from a query string

Discussion in 'PHP' started by alhermette, Sep 26, 2008.

  1. #1
    I need to pull some variables out of a query string. Normally I can do this easily using GET, however I have one variable that itself contains ampersands that have not been url encoded (and I have no control over this part). As a result GET does not fetch the whole variable.

    I thought that the best solution would be to take the whole query string and replace all the other variables (I know them all) with "" leaving me with just the one variable that I am after.

    What function do I use to replace multiple variables including the text of the variables as well?
     
    alhermette, Sep 26, 2008 IP
  2. fireflyproject

    fireflyproject Active Member

    Messages:
    969
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    70
    #2
    You will probably end up having to use str_replace a couple of times. Once for each variable.

    $var = str_replace(find,replace,string);
     
    fireflyproject, Sep 26, 2008 IP
  3. alhermette

    alhermette Peon

    Messages:
    74
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I tried that but hit a snag. The final variable that I want to be left with is a url and I picked a random one to do my testing, here's an example of a test query string:

    g=some_text&url=google.co.uk/search?hl=en&q=php+help&btnG=Google+Search&meta=&lastvar=something_else

    The bold part is the bit that I am trying to extract.

    I used str_replace like this

    $replace = array($g, "g=", $lastvar, "&lastvar=");
    $fullurl = str_ireplace($replace, '', $query_string);

    As you can see I am removing each variable that I do not want and also the text associated with that variable. The problem is that g= appears as text within the url that I am trying to extract so it got deleted inadvertently.

    I assume that I can remove a variable and the text associated with it without removing all instances of that text but I am not sure what format I would need to use in order to do that properly.
     
    alhermette, Sep 27, 2008 IP
  4. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #4
    just wrote this for you.
    $string = 'g=some_text&url=google.co.uk/search?hl=en&q=php+help&btnG=Google+Search&meta=&lastvar=something_else';
    
    $var_in_the_string = explode('&',$string);
    
    foreach($var_in_the_string as $key => $var) {
    $vars[$key] = explode('=',$var,2);
    }
    
    foreach($vars as $key=>$var) {
    $arranged[$var[0]] =$var[1];
    }
    PHP:
    $arranged (the array) will look like this after being printed using print_r():

    
    Array
    (
        [g] => some_text
        ['url'] => g=some_text&url=google.co.uk/search?hl=en&q=php+help&btnG=Google+Search&meta=&lastvar=something_else
        [q] => php+help
        [btnG] => Google+Search
        [meta] => 
        [lastvar] => something_else
    )
    
    Code (markup):
    i was not allowed to post URL yet so vB blocks me just now. Therefore i replace the square bracket URL with ['url'] above. It does not matter anyway.

    now you can easily call them: eg:
    url using $arranged['url']
    q using $arranged['q']
    etc.

    good luck
     
    ads2help, Sep 27, 2008 IP
  5. alhermette

    alhermette Peon

    Messages:
    74
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks for your help. Please forgive me for being slow but php is not my strong point and I am struggling to understand exactly what your code does. From what I see it may not do what I want.

    I need to extract three variables from the original query string. Two of them are easy using GET:

    g which will equal "some_text"
    lastvar which will equal "something_else"

    The third variable that I need to extract is

    url which should equal "google.co.uk/search?hl=en&q=php+help&btnG=Google+Search&meta="

    I do not need the variables q, btnG or meta as they are just part of the varible url as far as I am concerned. I may have missed something (if so please forgive me) but I don't understand how your code would achieve what I need.
     
    alhermette, Sep 27, 2008 IP
  6. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #6
    No, based on your string, URL is equal to google.co.uk/search?hl=en only.
    Next, the query typed by the user, q is equal to php+help ( since this is a google search query, the user must have typed: php help as the keyword.

    Next, btnG is equal to Google+Search
    and lastly meta is empty.

    Why? Because in PHP the & in URL act as the delimiter.
    Links like:
    index.php?act=lol
    Code (markup):
    $_GET['act'] carries the value lol

    Similarly,
    index.php?act=haha&data=5
    Code (markup):
    $_GET['act'] carries the value haha, and
    $_GET['data'] carries the value 5.
    $_GET['act'] DOES NOT carry the value haha&data=5

    Therefore, like what i said just now, you use the first code i gave you. It automatically process the URL string into array. If you want the URL= value, $arranged['url'] is carrying it.
     
    ads2help, Sep 27, 2008 IP
  7. alhermette

    alhermette Peon

    Messages:
    74
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Which is exactly why my problem is not so simple!

    The information that I want is not actually a simple variable, it is a full url which may or may not contain its own variables and I need the whole thing unaltered. As far as I can see the only way to do this is to start with the full query string and strip out the other components that I know will be there but even that does not seem to be so simple.

    I need a function that will replace both the value and text of each variable. See my former post for the problem that I encountered doing this.
     
    alhermette, Sep 27, 2008 IP