billyhafiz billyhafiz - 1 year ago 103
PHP Question

Is it possible to create a reliable php regex to get search query string from a Google url?

A possible url from Google may look like:

so the regex will produce: "user's search query".

Answer Source

Get the query string, then parse the vars into an array. It should always be q:

parse_str(parse_url($url, PHP_URL_QUERY), $result);

echo $result['q'];

The query string will be:


And the $result['q'] will be:

user's search query

And the full $result:

    [q] => user's search query
    [oq] => user's search query
    [gs_l] => serp.3
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download