PHP将字符串解析为多个数组

时间:2014-11-01 10:39:19

标签: php

我有这个字符串:

$str= "array((1, '31/10/2014 23:01:19', '5476743', 'name1', '89298.08', '679155', '5.895.804.754', '240', '679155', '5.895.804.754', '240')(1, '31/10/2014 23:01:29', '3724881', 'name2', '54141.48', '115663', '202.718.140', '20', '115663', '202.718.140', '20'));"

阵列可能有更多元素。我想解析得到这样的东西:

$array[0] = array(1, '31/10/2014 23:01:19', '5476743', 'name1', '89298.08', '679155', '5.895.804.754', '240', '679155', '5.895.804.754', '240'); 
$array[1] = array(1, '31/10/2014 23:01:29', '3724881', 'name2', '54141.48', '115663', '202.718.140', '20', '115663', '202.718.140', '20');
有人可以帮帮我吗? 感谢

3 个答案:

答案 0 :(得分:2)

你的API试图返回我想的PHP代码,但它不正确。如果您可以更改API以返回有效的PHP语法,并且您相信它不会向您发送恶意数据,那么这很容易:

$str= "array(array(1, '31/10/2014 23:01:19', '5476743', 'name1', '89298.08', '679155', '5.895.804.754', '240', '679155', '5.895.804.754', '240'),array(1, '31/10/2014 23:01:29', '3724881', 'name2', '54141.48', '115663', '202.718.140', '20', '115663', '202.718.140', '20'));";
$array = eval('return ' . $str);

print_r($array);

编辑:

如果您能够更改API代码,那么返回有效php代码的一种非常简单的方法是var_export()

答案 1 :(得分:0)

试试这个:

    function doit($str){
        $str=substr($str, 6,strlen($str)-8);
        $array1=explode(")(",')'.$str.'(');
        $count=0;
        foreach ($array1 as $key => $str1) {
            if(empty(trim($str1))){
                continue;
        }
        $str2=substr($str1, 1,strlen($str1)-1);
        $array2=explode(',', $str2);
        foreach ($array2 as $key1 => $one) {
            $tone=trim($one);
            if(empty($tone))continue;
            if(substr($tone, 0,1)=="'" && substr($tone, strlen($tone)-1,1)=="'"){
                $tone=substr($tone, 1,strlen($tone)-2);
            }
            $array3[$count][$key1]=$tone;
        }
        $count++;
    }
    return $array3;
}

结果:

Array
(
[0] => Array
    (
        [1] => 31/10/2014 23:01:19
        [2] => 5476743
        [3] => name1
        [4] => 89298.08
        [5] => 679155
        [6] => 5.895.804.754
        [7] => 240
        [8] => 679155
        [9] => 5.895.804.754
        [10] => 240
    )

[1] => Array
    (
        [1] => 31/10/2014 23:01:29
        [2] => 3724881
        [3] => name2
        [4] => 54141.48
        [5] => 115663
        [6] => 202.718.140
        [7] => 20
        [8] => 115663
        [9] => 202.718.140
        [10] => 20
    )

)

答案 2 :(得分:0)

在这里,我提出了一个不依赖于邪恶eval的代码,并且不会对包含逗号或反斜杠的值失败。

function _strpos_first_of_set($chrset,&$str,$offset)
{
    $pos = PHP_INT_MAX ;
    for($i=0;$i<strlen($chrset);$i++)
    {   
        $p = @strpos($str,$chrset{$i},$offset);
        if($p!==false)
            $pos=min($pos,$p);
    }
    return ($pos==PHP_INT_MAX ) ? false : $pos;
}

function doit(&$str)
{
    $offset = 6 ;/*array(*/
    $array = array();
    $ai = -1;
    $aj = 0;
    for(;;)
    {
        $p = @strpos($str,"(",$offset);
        if($p===false)
            return $array;
        $offset=$p+1;
        $array[++$ai] = array();
        $aj=0;
        for(;;)
        {
            $p = _strpos_first_of_set("',)",$str,$offset);
            if($p===false)
                return false;
            if($str{$p}==")")
            {
                $offset=$p+1;
                break;
            }
            if($str{$p}==",")
            {
                $array[$ai][$aj++] = trim(substr($str,$offset,$p-$offset));
                $offset = $p+1;
            }
            else
            {
                $offset=++$p;
                for(;;)
                {
                    $p = _strpos_first_of_set("'\\",$str,$p);
                    if($p===false)
                        return false;
                    if($str{$p}=="\\")
                        $p += 2;
                    else
                    {
                        $array[$ai][$aj++] = stripslashes(substr($str,$offset,$p-$offset));
                        $p = _strpos_first_of_set(",)",$str,$p+1);
                        if($p===false)
                            return false;
                        $offset = ($str{$p} == ",") ? $p+1 : $p ;
                        break;
                    }
                }
            }
        }
    }
}