Parse string as a query string

There has been an odd occasion in the past where I have used a string (Note: not the query string) in order to form a settings class in some way or another. I didn’t realise that there was a built in PHP function for this, and is only through reading through most of the PHP string functions that I came across it (and many other interesting functions).

The times of doing this are long gone (Am using the query string here just for clarity):

  1. <?php
  2. foreach (explode('&'$_SERVER['QUERY_STRING']) as $chunk) {
  3.     $chunk explode('='$chunk);
  4.     $variables[$chunk[0]] = $chunk[1];
  5. }

And I am now saying “hello” to this:

  1. <?php
  2. $variables parse_str($_SERVER['QUERY_STRING']);

Related articles

Top | Post a comment | Permalink

User submitted comments

1 John wrote

14th May, 2008


Cool. I wasnt aware of parse_string(). I seem to remember doing something similar to you, but from more of a configuration file.

2 Paul wrote

12th June, 2008


Really usful for something, i’ll work out what for one day. What application have you used it for out of interest?

3 Christopher Hill wrote

14th June, 2008


Paul, I have used the loooong method for one or two individual projects whilst at university. Like you, however, I have not used the parse_str() function, but I expect I will need to use it some time in the not so distant future.

4 peter wrote

15th June, 2008


i have never read the manual but i was thinking of soon, to find out things like this :-)


Penny for your thoughts

Comment posting guide

Your real name will be displayed as the author of the post. Real names are preferred, both first and second names, but nicknames/alter-egos are permitted. Any comments with the author name whose primary aim is to promote their website, and/or company will be removed.

If you supply a website URI then your real name will be clickable to that site. Only one sub-directory is permitted; e.g. http://www.example.org/directory.

Accepted comment input

  • <strong>…</strong>
  • <em>…</em>
  • <blockquote>…</blockquote>
  • <tt>…</tt>
  • <a href="…">…</a>
  • <code>…</code>




Recent articles

Other topics I ramble on about