If you want the parameters passed by query string to the currently executing script then you simply need:
$genres = preg_grep('!^genre!', array_keys($_GET));
$out = implode('_', $genres);
Here you're filtering out all the parameters that start with genre
using preg_grep()
and getting a list of parameter names using array_keys()
.
If you have a URL you need to parse then use this snippet:
$url = 'file.php?search=keyword+here&genre1=1&genre4=1&genre19=1&genre181&director=436&actor=347&search_rating=3';
$query = parse_url($url, PHP_URL_QUERY);
parse_str($query, $params);
$genres = preg_grep('!^genre!', array_keys($params));
echo implode('_', $genres);
The difference here is that you use parse_url()
to extract the query string and parse_str()
to parse the query string.
Output:
genre1_genre4_genre19_genre181