Sebastian Botez Sebastian Botez - 3 months ago 12
PHP Question

Regex. Find in string any PHP GET variables

Hello I'm trying to create a Regex that can find any GET variables from strings.
For example:

My function shoud return true. http://stackoverflow.com/questions/ask?title=test

Function shoud return false. http://stackoverflow.com/questions/ask

The pattern shoud find

?*anything*=


Any ideas? Thanks very much!

Answer

I suggests to take a look at parse_url function of php.

It breaks a url into components like path and query, so can check if a query exists in your uri or not, to get the path of uri etc.

In your example:

<?php
$uri = 'http://stackoverflow.com/questions/ask?title=test';
parse_url($uri, $parts);
echo $parts['query']; #title=test
echo $parts['path']; #'question/ask

if (isset($parts['query']) {
    return true;
}
Comments