从字符串中获取复杂的Object

时间:2015-12-13 11:18:42

标签: php regex

我有一个从REST Api URL获得的字符串,我想创建一个" PHP-readable"对象

字符串

field1=value1.order(DESC),value2.uppercase()&field2=value3.some('foo','bar')

结果

Object
(
    [field1] => Array
        (
            [value1] => Array
                (
                    [order] => Array
                        (
                            [0] => DESC
                        )
                )

            [value2] => Array
                (
                    [uppercase] => Array()
                )
        )
    [field2] => Array
        (
            [value3] => Array
                (
                    [some] => Array
                        (
                            [0] => 'foo',
                            [1] => 'bar'
                        )
                )
        )
)

我怎么能用PHP做到这一点?我以为我可以使用正则表达式。

更新

谢谢大家!我试图在嵌套的foreach语句中爆炸字符串,但是当我不得不用逗号进行爆炸时:

value3.some('foo','bar'),value4.some('bar','foo')

我获得了

Array
(
    [0] => value3.some('foo',
    [1] => 'bar'),
    [2] => value4.some('bar',
    [3] => 'foo')
)

我想得到:

Array
(
    [0] => value3.some('foo','bar'),
    [1] => value4.some('bar','foo')
)

2 个答案:

答案 0 :(得分:1)

PHP在线手册中名为Matt的用户创建了一个名为print_r_reverse()的函数 为了完整起见,这是输出的一个工作示例:

<?php
// shamelessly copied from http://www.php.net/manual/en/function.print-r.php#93529 (User Matt)
function print_r_reverse($in) {
    $lines = explode("\n", 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("\n", $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);

?>

答案 1 :(得分:0)

如果你仍然没有明白这个想法。你需要经历几次像这样的字符串爆炸

 $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);

你会得到这样的东西,

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)

明白了吗?做其余的事。