note 126789 added to function.parse-str

From: Date: Fri, 21 Jan 2022 21:20:14 +0000
Subject: note 126789 added to function.parse-str
Groups: php.notes 
Request: Send a blank email to php-notes+get-217530@lists.php.net to get a copy of this message
If you need the key names preserved and don't want spaces or . or unmatched [ or ] replaced by an underscore, yet you want all the other goodies of parse_str(), like turning matched [] into array elements, you can use this code as a base for that. <?php const periodPlaceholder = 'QQleQPunT'; const spacePlaceholder = 'QQleQSpaTIE'; function parse_str_clean($querystr): array { // without the converting of spaces and dots etc to underscores. $qquerystr = str_ireplace(['.','%2E','+',' ','%20'], [periodPlaceholder,periodPlaceholder,spacePlaceholder,spacePlaceholder,spacePlaceholder], $querystr); $arr = null ; parse_str($qquerystr, $arr); sanitizeKeys($arr, $querystr); return $arr; } function sanitizeKeys(&$arr, $querystr) { foreach($arr as $key=>$val) { // restore values to original $newval = $val ; if ( is_string($val)) { $newval = str_replace([periodPlaceholder,spacePlaceholder], ["."," "], $val); } $newkey = str_replace([periodPlaceholder,spacePlaceholder], ["."," "], $key); if ( str_contains($newkey, '_') ) { // periode of space or [ or ] converted to _. Restore with querystring $regex = '/&('.str_replace('_', '[ \.\[\]]', preg_quote($newkey, '/')).')=/'; $matches = null ; if ( preg_match_all($regex, "&".urldecode($querystr), $matches) ) { if ( count(array_unique($matches[1])) === 1 && $key != $matches[1][0] ) { $newkey = $matches[1][0] ; } } } if ( $newkey != $key ) { unset($arr[$key]); $arr[$newkey] = $newval ; } elseif( $val != $newval ) $arr[$key] = $newval; if ( is_array($val)) { sanitizeKeys($arr[$newkey], $querystr); } } } ?> For example: parse_str_clean("code.1=printr%28hahaha&code 1=448044&test.mijn%5B%5D%5B2%5D=test%20Roemer&test%5Bmijn=test%202e%20Roemer"); Produces: array(4) { ["code.1"]=> string(13) "printr(hahaha" ["code 1"]=> string(6) "448044" ["test.mijn"]=> array(1) { [0]=> array(1) { [2]=> string(11) "test Roemer" } } ["test[mijn"]=> string(14) "test 2e Roemer" } Whereas if you feed the same querystring to parse_str, you will get array(2) { ["code_1"]=> string(6) "448044" ["test_mijn"]=> string(14) "test 2e Roemer" } ---- Server IP: 45.112.84.4 Probable Submitter: 80.90.5.135 (proxied: 178.84.202.204) ---- Manual Page -- https://php.net/manual/en/function.parse-str.php Edit -- https://main.php.net/note/edit/126789 Del: integrated -- https://main.php.net/note/delete/126789/integrated Del: useless -- https://main.php.net/note/delete/126789/useless Del: bad code -- https://main.php.net/note/delete/126789/bad+code Del: spam -- https://main.php.net/note/delete/126789/spam Del: non-english -- https://main.php.net/note/delete/126789/non-english Del: in docs -- https://main.php.net/note/delete/126789/in+docs Del: other reasons-- https://main.php.net/note/delete/126789 Reject -- https://main.php.net/note/reject/126789 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#217530) next »