Permalink
Browse files

Documented parse_str equivalent.

  • Loading branch information...
1 parent 16d1dc5 commit 8b9e734caa1ffb1fb6cd6a3904d722058309fb3d Ronen Botzer committed with mnaberez May 11, 2010
Showing with 37 additions and 16 deletions.
  1. +37 −0 strings/parse_str.markdown
  2. +0 −16 strings/parse_str.markdown.unfinished
@@ -0,0 +1,37 @@
+# parse_str
+
+{{notice:
+ This function's solution requires the Addressable gem.
+}}
+
+PHP's `parse_str` function takes a URL-encoded query string and parses it
+into an associative array. The Ruby equivalent leverages Addressable's
+`query_values` method.
+
+{{code:php
+ parse_str("foo=bar", $output);
+ var_export($output);
+ // => array('foo' => 'bar')
+ parse_str("foo[]=1&foo[]=2&foo[]=3", $output);
+ var_export($output);
+ // array('foo' => array(0 => '1', 1 => '2', 2 => '3'))
+ parse_str("foo[a]=1&foo[b]=2", $output);
+ var_export($output);
+ // => array('foo' => array('a' => '1', 'b' => '2'))
+}}
+
+{{code:ruby
+ require 'addressable/uri'
+ Addressable::URI.parse("/?foo=bar").query_values()
+ // => {"foo"=>"bar"}
+ Addressable::URI.parse("/?foo[]=1&foo[]=2&foo[]=3").query_values()
+ // => {"foo"=>["1", "2", "3"]}
+ Addressable::URI.parse("/?foo[a]=1&foo[b]=2").query_values()
+ // => {"foo"=>{"a"=>"1", "b"=>"2"}}
+}}
+
+
+{{related:
+ url/http_build_query
+ strings/parse_url
+}}
@@ -1,16 +0,0 @@
-# parse_str
-
-{{code:php
-}}
-
-{{code:ruby
-}}
-
-
-{{related:
- strings/parse_url
- strings/pathinfo
- url/html_entity_decode
- strings/get_magic_quotes_gpc
- url/urldecode
-}}

0 comments on commit 8b9e734

Please sign in to comment.