Looking at the Rok solution, I came up with a version that addresses the limitations in my answer below, crb above and the Rok solution. See my improved version .
@crb's answer above is a good start, but there are a few problems.
- He recycles everything that is redundant; only those fields that have a "." the title needs to be recycled.
- It cannot handle arrays in the same way that native PHP processing does. for keys like "foo.bar []".
The solution below addresses both of these issues (note that it has been updated since publication). This is about 50% faster than my answer above in my testing, but will not handle situations where the data has the same key (or a key that is extracted the same way, for example, foo.bar and foo_bar are both extracted as foo_bar).
<?php public function fix2(&$target, $source, $keep = false) { if (!$source) { return; } preg_match_all( '/ # Match at start of string or & (?:^|(?<=&)) # Exclude cases where the period is in brackets, eg foo[bar.blarg] [^=&\[]* # Affected cases: periods and spaces (?:\.|%20) # Keep matching until assignment, next variable, end of string or # start of an array [^=&\[]* /x', $source, $matches ); foreach (current($matches) as $key) { $key = urldecode($key); $badKey = preg_replace('/(\.| )/', '_', $key); if (isset($target[$badKey])) { // Duplicate values may have already unset this $target[$key] = $target[$badKey]; if (!$keep) { unset($target[$badKey]); } } } }
El Yobo Aug 03 '13 at 1:54 on 2013-08-03 01:54
source share