Page 1 of 1

Regular Exp. Question / Request String Processing

Posted: Fri Dec 13, 2002 3:09 pm
by kitsch
So here's the task:

I have the following data:

Code: Select all

$referer = 'http://www.google.com.br/search?q=santa+claus+pictures&hl=pt&lr=&ie=UTF-8&oe=UTF-8&start=20&sa=N';
or

Code: Select all

$referer = 'http://www.google.com/search?hl=ro&inlang=pl&ie=ISO-8859-2&q=santa+claus+pictures&btnG=Caut%E3&lr=';
or

Code: Select all

$referer = 'http://www.google.com/search?hl=ro&inlang=pl&ie=ISO-8859-2&q=santa+claus+pictures';
or

Code: Select all

$referer = 'http://www.google.com/search?q=santa+claus+pictures';
(in others words i want to process all the cases in which a search engine can refer my site) and I would like to extract (match) the keywords. I know it's something like:

Code: Select all

preg_match($pattern, $referer, $matches);
$keywords = $matchesї1];
I just need the $pattern syntax which would match the keywords in all of my cases.

And is there a function of PHP to process the request strings into a more 'human friendly' form? Like replace + with space, and all those %XX characters with the corresponding alphanumeric data.

Thanks.

Posted: Fri Dec 13, 2002 3:57 pm
by BigE
Well... I personally don't write REGEX from scratch for someone... so you might want to start looking around php.net/pcre for a little info on how to get started.

Posted: Fri Dec 13, 2002 3:59 pm
by EvilWalrus
why not use parse_url() on thec $referer and do your extraction from teh query string? It's much more simple than using complex regexp to do it...

Posted: Fri Dec 13, 2002 4:07 pm
by kitsch
In fact I just need a solution to match anything that comes in a string after q= and lasts til & or the end of the string.

Posted: Fri Dec 13, 2002 6:14 pm
by kitsch
OK. Found the solution:

Code: Select all

preg_match('/q=(ї^&]+)/', $referer, $matches);
$keywords = urldecode($matchesї1]);
Hope it helps.