可靠地转换包含PHP数组信息,以字符串数组 [英] reliably convert string containing PHP array info to array

查看:108
本文介绍了可靠地转换包含PHP数组信息,以字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/3267951/simulate-php-array-language-construct-or-parse-with-regexp\">Simulate PHP数组语言结构或正则表达式解析?


假设我有字符串

  $海峡=阵列(1,3,4),阵列(阵列(4,5,6)),这是一个逗号,一个字符串内,阵列( ASDF'=&GT;'LALAL');

和我尝试爆炸成用逗号数组这使期望的最终结果是

  $爆炸[0] =阵列(1,3,4);
$爆炸[1] =阵列(阵列(4,5,6));
$爆炸[2] ='这是一个逗号,一个字符串内;
$爆炸[3] =阵列('ASDF'= GT;'LALAL');

简单地调用爆炸('',$ STR)是不会削减它,因为也有那些块中的逗号...

有没有办法能够可靠地爆炸这个即使有所需的数据块内的逗号。


解决方案

  

有没有办法能够可靠地爆炸这个即使有所需的块里面逗号?


PHP在默认情况下不提供这样的功能。然而,你有PHP的紧凑子集的字符串中和PHP提供了一些工具,在这里:一个的 PHP标记生成器 PHP解析器

因此​​,可能对您的字符串规范创建验证输入与允许令牌的辅助函数,然后分析它:

  $海峡=阵列(1,3,4),阵列(阵列(4,5,6)),这是一个逗号,一个字符串内,阵列( ASDF'=&GT;'LALAL');功能explode_string($ STR)
{
    $结果= NULL;    //验证字符串
    $的isValid = FALSE;
    $令牌= token_get_all(sprintf的('&LT; PHP%s'的,$ STR));
    array_shift($令牌);
    $有效=阵列(305,315,358,360,371,(,),,);
    的foreach($标记为$令牌)
    {
        列表($指数)=(阵列)$令牌;
        如果(!in_array($指数,$有效))
        {
            $的isValid = FALSE;
            打破;
        }
    }
    如果(!$的isValid)
        抛出新InvalidArgumentException('无效的字符串。');    //解析字符串
    $收益率=的eval(sprintf的(回归阵列(%S);',$ STR));    返回$返回;
}回声$海峡,\\ n;$结果= explode_string($海峡);后续代码var_dump($结果);

使用的标记是:

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

令牌索引号可以通过使用<一给予令牌名 HREF =htt​​p://de2.php.net/manual/en/function.token-name.php相对=nofollow> TOKEN_NAME

它给你(演示):

 阵列

    [0] =&GT;排列
        (
            [0] =&GT; 1
            [1] =&GT; 3
            [2] =&GT; 4
        )    [1] =&GT;排列
        (
            [0] =&GT;排列
                (
                    [0] =&GT; 4
                    [1] =&GT;五
                    [2] =&GT; 6
                )        )    [2] =&GT;这是一个逗号,一个字符串内
    [3] =&GT;排列
        (
            [航空自卫队] =&GT; LALAL
        ))

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

解决方案

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, "\n";

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

)

这篇关于可靠地转换包含PHP数组信息,以字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆