dongshanjin8947 2011-10-24 09:08
浏览 26
已采纳

可靠地将包含PHP数组信息的字符串转换为数组[duplicate]

Possible Duplicate:
Simulate php array language construct or parse with regexp?

suppose I have the string

$str = "array(1,3,4),array(array(4,5,6)),'this is a comma , inside a string',array('asdf' => 'lalal')";

and I try to explode this into an array by comma so that the desired end result is

$explode[0] =  array(1,3,4);
$explode[1] = array(array(4,5,6));
$explode[2] = 'this is a comma , inside a string';
$explode[3] = array('asdf' => 'lalal');

simply calling explode(',',$str) is not going to cut it since there are also commas within those chunks...

is there a way to explode this reliably even if there is commas inside the desired chunks

  • 写回答

2条回答 默认 最新

  • duanchi6377 2011-10-24 10:33
    关注

    is there a way to explode this reliably even if there is commas inside the desired chunks?

    PHP by default does not provide such a function. However you have a compact subset of PHP inside your string and PHP offers some tools here: A PHP tokenizer and a PHP parser.

    Therefore it's possible for your string specification to create a helper function that validates the input against allowed tokens and then parse it:

    $str = "array(1,3,4),array(array(4,5,6)),'this is a comma , inside a string', array('asdf' => 'lalal')";
    
    function explode_string($str)
    {
        $result = NULL;
    
        // validate string
        $isValid = FALSE;
        $tokens = token_get_all(sprintf('<?php %s', $str));
        array_shift($tokens);
        $valid = array(305, 315, 358, 360, 371, '(', ')', ',');
        foreach($tokens as $token)
        {
            list($index) = (array) $token;
            if (!in_array($index, $valid))
            {
                $isValid = FALSE;
                break;
            }
        }
        if (!$isValid)
            throw new InvalidArgumentException('Invalid string.');
    
        // parse string
        $return = eval(sprintf('return array(%s);', $str));
    
        return $return;
    }
    
    echo $str, "
    ";
    
    $result = explode_string($str);
    
    var_dump($result);
    

    The tokens used are:

    T_LNUMBER (305)
    T_CONSTANT_ENCAPSED_STRING (315)
    T_DOUBLE_ARROW (358)
    T_ARRAY (360)
    T_WHITESPACE (371)
    

    The token index number can be given a token name by using token_name.

    Which gives you (Demo):

    Array
    (
        [0] => Array
            (
                [0] => 1
                [1] => 3
                [2] => 4
            )
    
        [1] => Array
            (
                [0] => Array
                    (
                        [0] => 4
                        [1] => 5
                        [2] => 6
                    )
    
            )
    
        [2] => this is a comma , inside a string
        [3] => Array
            (
                [asdf] => lalal
            )
    
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多