You can use the amazing
parse_str() function. It does everything you need. But you are using different delimiters for
key => value pairs. If you change those to
=, you can do like this:
$filtersStr = 'id=1&name=alex';
$filters will have what you want. But that's not the case, so, one could do this:
array('%', '=', '&', '||', ':'),
array('%26', '%3D', '%26', '&', '='),
& will have a special meaning. They have to be replaced with their URL-codes.
parse_str() works on URL-encoded strings and leaving those characters unchanged would cause all sorts of bugs. The order of the elements in the array is VERY important.
Here's how it works:
It must be the first of you will be replacing
% everywhere and break it
This guarantees that no meaningful
= will be replaced
At this point, your
'id:1||name:alex' will be
- Hand it over to
And done! I prefer the first method instead of this kludge. Use this second one with extreme care!
In a comment, it was explained that it's usage was like this:
If you can change the URL, throw this whole code away and do like this:
This will have the exact structure you wish. Simply use
$_GET['filter'] to access all values. And done! An alternative with 0 lines of code!