note 52588 added to function.parse-str

From: Date: Fri, 06 May 2005 15:13:14 +0000
Subject: note 52588 added to function.parse-str
Groups: php.notes 
Request: Send a blank email to php-notes+get-89003@lists.php.net to get a copy of this message
You may want to parse the query string into an array. <?php /** * Similar to parse_str. Returns false if the query string or URL is empty. Because we're not parsing to * variables but to array key entries, this function will handle ?[]=1&[]=2 "correctly." * * @return array Similar to the $_GET formatting that PHP does automagically. * @param string $url A query string or URL * @param boolean $qmark Find and strip out everything before the question mark in the string */ function parse_query_string($url, $qmark=true) { if ($qmark) { $pos = strpos($url, "?"); if ($pos !== false) { $url = substr($url, $pos + 1); } } if (empty($url)) return false; $tokens = explode("&", $url); $urlVars = array(); foreach ($tokens as $token) { $value = string_pair($token, "=", ""); if (preg_match('/^([^\[]*)(\[.*\])$/', $token, $matches)) { parse_query_string_array($urlVars, $matches[1], $matches[2], $value); } else { $urlVars[urldecode($token)] = urldecode($value); } } return $urlVars; } /** * Utility function for parse_query_string. Given a result array, a starting key, and a set of keys formatted like "[a][b][c]" * and the final value, updates the result array with the correct PHP array keys. * * @return void * @param array $result A result array to populate from the query string * @param string $k The starting key to populate in $result * @param string $arrayKeys The key list to parse in the form "[][a][what%20ever]" * @param string $value The value to place at the destination array key */ function parse_query_string_array(&$result, $k, $arrayKeys, $value) { if (!preg_match_all('/\[([^\]]*)\]/', $arrayKeys, $matches)) return $value; if (!isset($result[$k])) { $result[urldecode($k)] = array(); } $temp =& $result[$k]; $last = urldecode(array_pop($matches[1])); foreach ($matches[1] as $k) { $k = urldecode($k); if ($k === "") { $temp[] = array(); $temp =& $temp[count($temp)-1]; } else if (!isset($temp[$k])) { $temp[$k] = array(); $temp =& $temp[$k]; } } if ($last === "") { $temp[] = $value; } else { $temp[urldecode($last)] = $value; } } /** * Breaks a string into a pair for a common parsing function. * * The string passed in is truncated to the left half of the string pair, if any, and the right half, if anything, is returned. * * An example of using this would be: * <code> * $path = "Account.Balance"; * $field = string_pair($path); * * $path is "Account" * $field is "Balance" * * $path = "Account"; * $field = string_pair($path); * * $path is "Account" * $field is false * </code> * * @return string The "right" portion of the string is returned if the delimiter is found. * @param string $a A string to break into a pair. The "left" portion of the string is returned here if the delimiter is found. * @param string $delim The characters used to delimit a string pair * @param mixed $default The value to return if the delimiter is not found in the string * @desc */ function string_pair(&$a, $delim='.', $default=false) { $n = strpos($a, $delim); if ($n === false) return $default; $result = substr($a, $n+strlen($delim)); $a = substr($a, 0, $n); return $result; } ?> ---- Manual Page -- http://www.php.net/manual/en/function.parse-str.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+52588 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+52588&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+52588&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+52588&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+52588&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+52588&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+52588&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#89003) next »