Regular Exp. Question / Request String Processing

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kitsch
Forum Newbie
Posts: 12
Joined: Mon Nov 11, 2002 3:29 pm

Regular Exp. Question / Request String Processing

Post 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.
User avatar
BigE
Site Admin
Posts: 139
Joined: Fri Apr 19, 2002 9:49 am
Location: Missouri, USA
Contact:

Post 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.
User avatar
EvilWalrus
Site Admin
Posts: 209
Joined: Thu Apr 18, 2002 3:21 pm
Location: Springmont, PA USA

Post 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...
kitsch
Forum Newbie
Posts: 12
Joined: Mon Nov 11, 2002 3:29 pm

Post 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.
kitsch
Forum Newbie
Posts: 12
Joined: Mon Nov 11, 2002 3:29 pm

Post by kitsch »

OK. Found the solution:

Code: Select all

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