NamemeNick Posted April 5, 2009 Share Posted April 5, 2009 This seems like a simple problem but I'm a newbie struggling What is the most efficient way to extract key-value pairs from a given string? For instance I'd like to go from: 'height=6&weight=170&eyes=brown' to $height='6'; $weight='170'; $eyes='brown'; Please teach :-) Quote Link to comment Share on other sites More sharing options...
Mark Baker Posted April 5, 2009 Share Posted April 5, 2009 $string = 'height=6&weight=170&eyes=brown'; $tmpArray = explode('&',$string); foreach ($tmpArray as $tmpVar) { list($name,$value) = explode('=',$tmpVar); $$name = $value; } Quote Link to comment Share on other sites More sharing options...
ghostdog74 Posted April 5, 2009 Share Posted April 5, 2009 $string="height=6&weight=170&eyes=brown"; $s = explode("&",$string); foreach( $s as $k=>$v){ $v=explode("=",$v); $a[$v[0]]=$v[1]; } print_r($a); Quote Link to comment Share on other sites More sharing options...
NamemeNick Posted April 5, 2009 Author Share Posted April 5, 2009 Thanks guys! ghostdog74, it seems like your script will return an array rather than distinct variables holding strings. Is that right? What do you think of this one? I got it from another source but it looks like gibberish to me (seems to work though) $querystring = 'height=6&weight=170&eyes=brown'; preg_match_all("/(.*?)=(.*?)(&|$)/", $querystring, $matches); for ($i = 0; $i < count($matches[0]); $i++) $$matches[1][$i] = $matches[2][$i]; Quote Link to comment Share on other sites More sharing options...
ghostdog74 Posted April 5, 2009 Share Posted April 5, 2009 ghostdog74, it seems like your script will return an array rather than distinct variables holding strings. Is that right? yes, that's right. if you don't need arrays, then after exploding on the "&", the array will contain 3 elements. that's all you need. What do you think of this one? I got it from another source but it looks like gibberish to me (seems to work though) $querystring = 'height=6&weight=170&eyes=brown'; preg_match_all("/(.*?)=(.*?)(&|$)/", $querystring, $matches); for ($i = 0; $i < count($matches[0]); $i++) $$matches[1][$i] = $matches[2][$i]; if you don't understand what its doing in the first place, then don't use it. regular expressions , although powerful, tends to make things look "gibberish". Even worse if you are not well trained in regex. Being more expressive in your codes will make your life easier next time, both in the area of maintenance and code troubleshooting. Quote Link to comment Share on other sites More sharing options...
NamemeNick Posted April 6, 2009 Author Share Posted April 6, 2009 Thanks ghostdog, Someone brought parse_str() to my attention. Checkout http://us2.php.net/manual/en/function.parse-str.php Quote Link to comment Share on other sites More sharing options...
ghostdog74 Posted April 6, 2009 Share Posted April 6, 2009 Thanks ghostdog, Someone brought parse_str() to my attention. Checkout http://us2.php.net/manual/en/function.parse-str.php good. the bottom line is, PHP provides you numerous string functions you can use, regex should be the last thing that comes to your mind. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.