note 53921 added to faq.html

From: Date: Fri, 17 Jun 2005 04:17:23 +0000
Subject: note 53921 added to faq.html
Groups: php.notes 
Request: Send a blank email to php-notes+get-90566@lists.php.net to get a copy of this message
Well, I was working on this one project, on the assumption that I could get the values of all elements with the same name from an appropriately named array. Well, I was *very* disappointed when I couldn't, so I made it so I did anyway. The following script should convert the raw post data to a $_POST variable, with form data from SELECT elements and their ilk being transformed into an array. It's heavily unoptimized, and I probably missed something, but it's relatively easy to read. I welcome corrections. <?php if ($_POST) { $postdata = file_get_contents('php://input'); $uglybugger = '/(?<=&)([^&=]+)(?:=([^&]*))/'; $matches = array(); preg_match_all($uglybugger, $postdata, $matches); $_POST = array(); $match_count = count($matches[0]); for ($i = 0; $i < $match_count; $i++) { if (!isset($_POST[$matches[1][$i]])) { $_POST[$matches[1][$i]] = array(); } $_POST[$matches[1][$i]][] = $matches[2][$i]; } $match_count = count($_POST); for ($i = 0; $i < $match_count; $i++) { if (count($_POST[$i]) == 1) { $_POST[$i] = $_POST[$i][0]; } } } ?> ---- Manual Page -- http://www.php.net/manual/en/faq.html.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+53921 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+53921&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+53921&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+53921&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+53921&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+53921&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+53921&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+53921&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#90566) next »