I have a string obtained from a REST Api URL and I want to create a "PHP-readable" object.
String
field1=value1.order(DESC),value2.uppercase()&field2=value3.some('foo','bar')
Result
Object
(
[field1] => Array
(
[value1] => Array
(
[order] => Array
(
[0] => DESC
)
)
[value2] => Array
(
[uppercase] => Array()
)
)
[field2] => Array
(
[value3] => Array
(
[some] => Array
(
[0] => 'foo',
[1] => 'bar'
)
)
)
)
How could I do that in PHP? I thought I could use a regex pattern.
Thank you all! I tried to explode strings in nested foreach statements, but when I have to explode with commas:
value3.some('foo','bar'),value4.some('bar','foo')
I obtain
Array
(
[0] => value3.some('foo',
[1] => 'bar'),
[2] => value4.some('bar',
[3] => 'foo')
)
I would like to get:
Array
(
[0] => value3.some('foo','bar'),
[1] => value4.some('bar','foo')
)
If you still didn't get the idea have a look on this. You need to go through exploding the string several times something like this,
$string = "field1=value1.order(DESC),value2.uppercase()&field2=value3.some('foo','bar')";
$str = explode("&", $string);
foreach ($str as $key => $value){
$str2[] = explode("=", $value);
$field[$str2[$key][0]][] = $str2[$key][1];
}
var_dump($field);
And you'll get something like this,
array (size=2)
'field1' =>
array (size=1)
0 => string 'value1.order(DESC),value2.uppercase()' (length=37)
'field2' =>
array (size=1)
0 => string 'value3.some('foo','bar')' (length=24)
get the idea? do the rest.
A user called Matt at the PHP online manual has created a function called print_r_reverse()
.
For the sake of completeness, here's a working example with your output:
<?php
// shamelessly copied from http://www.php.net/manual/en/function.print-r.php#93529 (User Matt)
function print_r_reverse($in) {
$lines = explode("
", trim($in));
if (trim($lines[0]) != 'Array') {
// bottomed out to something that isn't an array
return $in;
} else {
// this is an array, lets parse it
if (preg_match("/(\s{5,})\(/", $lines[1], $match)) {
// this is a tested array/recursive call to this function
// take a set of spaces off the beginning
$spaces = $match[1];
$spaces_length = strlen($spaces);
$lines_total = count($lines);
for ($i = 0; $i < $lines_total; $i++) {
if (substr($lines[$i], 0, $spaces_length) == $spaces) {
$lines[$i] = substr($lines[$i], $spaces_length);
}
}
}
array_shift($lines); // Array
array_shift($lines); // (
array_pop($lines); // )
$in = implode("
", $lines);
// make sure we only match stuff with 4 preceding spaces (stuff for this array and not a nested one)
preg_match_all("/^\s{4}\[(.+?)\] \=\> /m", $in, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
$pos = array();
$previous_key = '';
$in_length = strlen($in);
// store the following in $pos:
// array with key = key of the parsed array's item
// value = array(start position in $in, $end position in $in)
foreach ($matches as $match) {
$key = $match[1][0];
$start = $match[0][1] + strlen($match[0][0]);
$pos[$key] = array($start, $in_length);
if ($previous_key != '') $pos[$previous_key][1] = $match[0][1] - 1;
$previous_key = $key;
}
$ret = array();
foreach ($pos as $key => $where) {
// recursively see if the parsed out value is an array too
$ret[$key] = print_r_reverse(substr($in, $where[0], $where[1] - $where[0]));
}
return $ret;
}
}
// here comes your input
$str = "Object
(
[field1] => Array
(
[value1] => Array
(
[order] => Array
(
[0] => DESC
)
)
[value2] => Array
(
[uppercase] => Array()
)
)
[field2] => Array
(
[value3] => Array
(
[some] => Array
(
[0] => 'foo',
[1] => 'bar'
)
)
)
)";
$t = print_r_reverse($str);
// only to check if it works:
print_r($t);
?>