Capturing search keywords in php

In awstats, I get a table with all the keywords and phrases used to find my site. I would like to capture this myself, however each search engine url is in a different format. When google is a referent, I can use the q variable from the querystring as the search term (for example google.com?q=my+keywords), however another search engine can have the format searchengine.com?search=my+keywords

Is there a general way to define keywords? Or do I need to create a regex / filter for each search engine?

+1


source to share


2 answers


One possibility is to simply grab the referenced URL ( $_SERVER['HTTP_REFERER']

) and parse the keywords in it.

For example check this google url (search for "stack overflow"):



http://www.google.com/search?hl=en&q=stack+overflow&aq=0&oq=stack+over&aqi=g10

      

The value of the q

GET variable contains keywords separated by + characters.

+4


source


I need to keep adding it, but here is REGEX, which should separate keywords from google, yahoo, bing, ask, and MSN (just like Bing). It leaves + in between, but this should be a good place for you:



.*(\?p=|\?q=|&q=|\?s=)([a-zA-Z0-9 +]*)(&toggle=|&ie=utf-8|&FORM=|&aq=|&x=|&gwp).

      

0


source







All Articles