views:

110

answers:

1

I want to extract text from $_SERVER['HTTP_REFERER']

let's say

$_SERVER['HTTP_REFERER'] = http://www.google.com/search?source=ig&hl=en&rlz=&q=something+i+am+looking+for&aq=f&oq=&aqi=

then I want $query equal "something+i+am+looking+for". I figure I can use pregreplace so I can say

$query=preg_replace([some regex], ,$_SERVER['HTTP_REFERER']);

And the regex should mean "anything that is after '&q=' and before '&aq'". How can I write this as regex?

A: 

Sounds like you are using php. Use parse_url instead.

$link = "http://www.google.com/search?source=ig&hl=en&rlz=&q=something+i+am+looking+for&aq=f&oq=&aqi=";
$url  = parse_url($link);
$query = $url['query'];
parse_str($query, $result);

echo $result['q'];
Byron Whitlock
thank you for helping me out!
pg