note 105746 added to function.parse-str
| From: | nhl261 at gmail dot com | Date: | Mon, 12 Sep 2011 10:22:57 +0000 |
| Subject: | note 105746 added to function.parse-str | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-183104@lists.php.net to get a copy of this message | ||
class N1QS {
protected $_data = array();
public function __construct($rawqs = '') {
$this->parse($rawqs);
}
public function parse($rawqs) {
if ($rawqs == '') return;
$this->_data = array();
// replace & if there are some
if (strpos($rawqs, '&')) $rawqs = str_replace('&',
'&', $rawqs);
$pairs = explode('&', $rawqs);
for ($i = 0, $c = count($pairs); $i < $c; $i++) {
$parts = explode('=', $pairs[$i], 2);
$key = urldecode($parts[0]);
$val = '';
// skip if main key is empty
if (trim($key) == '') continue;
if (isset($parts[1])) $val = urldecode($parts[1]);
$osb_pos = strpos($key, '[');
if ($osb_pos !== false && $osb_pos !== 0) {
$csb_pos = strpos($key, ']', $osb_pos);
if ($csb_pos !== false) {
$mk = substr($key, 0, $osb_pos);
$sk = substr($key, $osb_pos + 1, $csb_pos - $osb_pos - 1);
$ex = (string)substr($key, $csb_pos + 1);
$rmk = $this->_isNum($mk) ? (int)$mk : $mk;
if (!isset($this->_data[$rmk]) || !is_array($this->_data[$rmk])) $this->_data[$rmk] =
array();
if ($sk == '') {
$this->_data[$rmk][] = $this->_setVal(null, $ex, $val);
} else {
$rsk = $this->_isNum($sk) ? (int)$sk : $sk;
if (!array_key_exists($rsk, $this->_data[$rmk])) $this->_data[$rmk][$rsk] = null;
$this->_data[$rmk][$rsk] = $this->_setVal($this->_data[$rmk][$rsk], $ex, $val);
}
} else {
$this->_data[$key] = $val;
}
} else {
$this->_data[$key] = $val;
}
} // for
}
protected function _setVal($ctx, $aex, $val) {
if ($aex == '') return $val;
$osb_pos = strpos($aex, '[');
if ($osb_pos === 0) {
$csb_pos = strpos($aex, ']', $osb_pos);
if ($csb_pos !== false) {
$sk = substr($aex, $osb_pos + 1, $csb_pos - $osb_pos - 1);
$ex = (string)substr($aex, $csb_pos + 1);
if (!isset($ctx) || !is_array($ctx)) $ctx = array();
if ($sk == '') {
$ctx[] = $this->_setVal(null, $ex, $val);
} else {
$rsk = $this->_isNum($sk) ? (int)$sk : $sk;
if (!array_key_exists($rsk, $ctx)) $ctx[$rsk] = null;
$ctx[$rsk] = $this->_setVal($ctx[$rsk], $ex, $val);
}
return $ctx;
} else {
return $val;
}
} else {
return $val;
}
}
protected function _isNum($value) {
if (preg_match('/^\-?[1-9]\d*$/', $value)) {
return true;
} else {
return false;
}
}
public function data() {
return $this->_data;
}
} //class
$qs = new N1QS();
$qs->parse('Lorem+ipsum=something+to+test&I.m.arr[12]=456&arr-2[key]=val&1=1&6[]=1&6[]=2');
var_dump($qs->data());
----
Server IP: 69.147.83.197
Probable Submitter: 115.74.13.206
----
Manual Page -- http://www.php.net/manual/en/function.parse-str.php
Edit -- https://master.php.net/note/edit/105746
Del: integrated -- https://master.php.net/note/delete/105746/integrated
Del: useless -- https://master.php.net/note/delete/105746/useless
Del: bad code -- https://master.php.net/note/delete/105746/bad+code
Del: spam -- https://master.php.net/note/delete/105746/spam
Del: non-english -- https://master.php.net/note/delete/105746/non-english
Del: in docs -- https://master.php.net/note/delete/105746/in+docs
Del: other reasons-- https://master.php.net/note/delete/105746
Reject -- https://master.php.net/note/reject/105746
Search -- https://master.php.net/manage/user-notes.php