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 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题