PHP:如何把包含在实际的阵列中的前pression一个字符串? [英] PHP: How to turn a string that contains an array expression in an actual array?

查看:106
本文介绍了PHP:如何把包含在实际的阵列中的前pression一个字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有用户输入($的ATT)为重点=>值对的数组。一些值可以写成一个阵列的前pression,如:

I have an array of user inputs ($atts) as key=>value pairs. Some of the values could be written as an array expression, such as:

'setting' => 'array(50,25)'

在这种情况下,我想包含在该字符串数组前pression转换为实际的数组。所以输出会是这样的:

In those cases, I would like to convert the array expression contained in that string into an actual array. So the output would be something like:

$atts = array(
'setting' => array(50,25),
'another' => 'not written as an array expression'
)

写逻辑中,code将是:

Written logically, the code would be:

有关每个键=>值对数组$ ...的ATT
如果该值是格式化为一个阵列的前pression字符串...
爆炸的价值到一个数组。

For each key=>value pair in the array $atts... if the value is a string formatted as an array expression... explode that value into an array.

任何人都知道我会如何在PHP写的吗?

Anybody know how I would write this in PHP?

推荐答案

使用标记生成器

function stringToArray($str) {
    $array = array();
    $toks = token_get_all("<?php $str");

    if ($toks[1][0] != T_ARRAY || $toks[2] != '(' || end($toks) != ')')
        return null;

    for($i=3; $i<count($toks)-1; $i+=2) {
        if (count($toks[$i]) != 3)
            return null;

        if ($toks[$i][0] == T_WHITESPACE) {
            $i--;
            continue;
        }

        if ($toks[$i][0] == T_VARIABLE || $toks[$i][0] == T_STRING)
            return null;

        $value = $toks[$i][1];
        if ($toks[$i][0] == T_CONSTANT_ENCAPSED_STRING)
            $value = substr($value, 1, strlen($value) - 2);

        $array[] = $value;

        if ($toks[$i + 1] != ',' && $toks[$i + 1] != ')' && $toks[$i + 1][0] != T_WHITESPACE)
            return null;
    }

    return $array;
}

以上将只对文字。传递一个变量,常量,一个前pression,嵌套数组或畸形的数组声明将返回

stringToArray('array(1,2)');              // works
stringToArray('array(1,2.4)');            // works
stringToArray('array("foo",2)');          // works
stringToArray('array(\'hello\',2)');      // works
stringToArray('array()');                 // works
stringToArray('array(1,2 + 3)');          // returns null
stringToArray('array(1,2 + 3)');          // returns null
stringToArray('array("foo"."bar")');      // returns null
stringToArray('array(array("hello"))');   // returns null
stringToArray('array($a,$b)');            // returns null
stringToArray('array(new bar)');          // returns null
stringToArray('array(SOME_CONST)');       // returns null
stringToArray('hello');                   // returns null

您也可以使用下面的检查,如果你的字符串是一个数组前pression与否:

You can also use the following to check if your string is an array expression or not:

function isArrayExpression($str) {
    $toks = token_get_all("<?php $str");
    return (
        $toks[1][0] == T_ARRAY &&
        $toks[2] == '(' &&
        end($toks) == ')'
    );
}

isArrayExpression('array(1,2,3)');         // true
isArrayExpression('array is cool');        // false
isArrayExpression('array(!!!!');           // false

您可以随时调整它自己的需要。希望这有助于。

You can always tweak it to your needs. Hope this helps.

这篇关于PHP:如何把包含在实际的阵列中的前pression一个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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