preg_match help

Discussion in 'PHP' started by ThePirate, Sep 18, 2007.

  1. #1
    Hey everyone,

    I have a little preg_match problem that I can't seem to solve, I'm pretty new to php and programming, and haven't got my head round regular expressions yet!

    What I have is this:

    preg_match("'q=(.*?)(&| )'si", " $url ", $keywords);

    I am using it to match/extract the q= parameter (search term) from the referrer when a surfer clicks from google. The problem is that google also occasionally has a aq= parameter which is usually set to aq=t. In these cases the search term is logged as t rather than the actual term.

    I know that I need to basically check for ?q= or &q= to make sure the parameter is just q= but I can't work out how I fit that into my expression above. Any help would be greatly appreciated.

    Thanks
     
    ThePirate, Sep 18, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    preg_match('/[&\?]q=([^&]+)/', $url, $keywords);
    
    echo $keywords[1];
    
    PHP:
     
    nico_swd, Sep 18, 2007 IP
    ThePirate likes this.
  3. ThePirate

    ThePirate Peon

    Messages:
    40
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Genius thanks! Works a treat. Rep added
     
    ThePirate, Sep 18, 2007 IP