将字符串内容放入数组php中

时间:2014-02-18 09:55:28

标签: php arrays string

我有这个字符串

string(2091) "


"roots" => array(
    array(
        "driver"        => "LocalFileSystem",   // driver for accessing file system (REQUIRED)
        "path"          => "../files/",         // path to files (REQUIRED)
        //"URL"           => dirname($_SERVER["PHP_SELF"]) . "/../files/", // URL to files (REQUIRED)
        "accessControl" => "access" ,            // disable and hide dot starting files (OPTIONAL)
        "alias" => "Root",
        //"uploadDeny"    => array("all"),
        "attributes" => array(
    array(
        "pattern" => "/\manuali$/",  //You can also set permissions for file types by adding, for example, <b>.jpg</b> inside pattern.
       // "pattern" =>"/\PROVA$/",
        "read" => true,
        "write" => false,
        "locked" => true,
    ),
    array(
        "pattern" => "/rapporti$/",  //You can also set permissions for file types by adding, for example, <b>.jpg</b> inside pattern.
       // "pattern" =>"/\PROVA$/",
        "read" => true,
        "write" => true,
        "locked" => true,
    )
     [...]
     ";

我想将数组的整个值放入数组中。例如:

array(


"roots" => array(
    array(
        "driver"        => "LocalFileSystem",   // driver for accessing file system (REQUIRED)
        "path"          => "../files/",         // path to files (REQUIRED)
        //"URL"           => dirname($_SERVER["PHP_SELF"]) . "/../files/", // URL to files (REQUIRED)
        "accessControl" => "access" ,            // disable and hide dot starting files (OPTIONAL)
        "alias" => "Root",
        //"uploadDeny"    => array("all"),
        "attributes" => array(
    array(
        "pattern" => "/\manuali$/",  //You can also set permissions for file types by adding, for example, <b>.jpg</b> inside pattern.
       // "pattern" =>"/\PROVA$/",
        "read" => true,
        "write" => false,
        "locked" => true,
    ),
    array(
        "pattern" => "/rapporti$/",  //You can also set permissions for file types by adding, for example, <b>.jpg</b> inside pattern.
       // "pattern" =>"/\PROVA$/",
        "read" => true,
        "write" => true,
        "locked" => true,
    )
     [...]
     );

我已经尝试了str_split() implode() array_push() ...但是每个函数都将此字符串放入数组array(string(" "))我希望置于此模式array(" ")

谢谢

2 个答案:

答案 0 :(得分:1)

我能想到两种主要方法。

一,是使用preg_match使用某种复杂的模式匹配循环。您可以使用explode\n将字符串拆分为每一行,将其转换为数组(每行),循环遍历每一行,并使用模式匹配(正则表达式)来确定做它。

第二种方法非常危险,所以我强烈建议你不要使用它,但php有它自己的eval函数,它可以让PHP将字符串解释为实际的PHP。

您需要在字符串中附加变量赋值,例如

$arrayString = "$var = array(' . $arrayString . ');";

然后

eval($arrayString);

但是,如果可以的话,你真的不想这样做。来自PHP文档:

  

注意

     

eval()语言结构非常危险,因为它允许   执行任意PHP代码。因此不鼓励使用它。如果你   仔细确认除了使用它之外别无选择   构建时,要特别注意不要传递任何用户提供的数据   没有事先正确验证它就进入它。

答案 1 :(得分:1)

如果您的字符串是可解析的,并且您不介意可能的注入,那么您只需eval数组表示。

例如,如果您的字符串位于$arrayStr,并且您想要从中创建数组$myArray

$arrayStr = '"roots" => array ( ... )';
eval('$myArray = array(' . $arrayStr . ');');

请注意,如果$arrayStr并非完全由控制,从生成到转换,则风险很高,因为其中的任何内容都会被评估。

在应用程序的各个部分之间交换整个数组的更好的解决方案是事先serialize()来创建可以存储的数组/对象的字符串表示,然后在需要时将其反序列化它的原始形式。