zamrg Posted November 23, 2010 Share Posted November 23, 2010 I've been struggling to parse a response like key1=value1;key2=value2;key3=value3, something like getclientsdetails and a few other functions spit out. It would normally be something as simple as a split but the fact that any one of these value's can contain a ; raises an issue. 0 Quote Link to comment Share on other sites More sharing options...
zamrg Posted November 23, 2010 Author Share Posted November 23, 2010 I managed to solve it. I'm using the current function; which will safely convert key=value to an associative array whilst still keeping any ; within values intact. private function _parse_vars($str) { $results = array(); $segments = explode(';', $str); $last_key = ''; foreach ($segments as $segment) { if (preg_match('/^([a-z0-9_]+)=(.*)$/i', $segment, $matches)) { $last_key = $matches[1]; $results[$last_key] = $matches[2]; } else { $results[$last_key] .= ';' . $segment; } } return $results; } 0 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.