0

I have this 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,
    )
     [...]
     ";

i want to put into array the entire value of a string. Ex:

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,
    )
     [...]
     );

i have tried str_split() implode() array_push()... but every function put the string into array in this mode array(string(" "))i want to put in this mode array(" ").

Thanks

4
  • 1
    What is producing that string in the first place? Can't you just serialize it instead of just printing it as a string and then trying to parse a string back into a complex structure
    – Anigel
    Feb 18, 2014 at 9:58
  • the string are a reading from a file ex. $xx=file_get_contents('out.txt'); Feb 18, 2014 at 10:07
  • 1
    The question still stands what is generating that string in that file. It would make more sense for that file to just contain the serialized structure rather than a string.
    – Anigel
    Feb 18, 2014 at 10:08
  • Stab in the dark here, but could require or include not work if it's in a separate file? Would need to prepend an open php tag and variable assignment, and then append a close php tag on to it though.. However, I guess this is just the same as evalling it, not really the best idea.
    – Sean
    Feb 18, 2014 at 10:12

2 Answers 2

1

If your string is parseable and you don't mind about possible injections, you can simply eval your array representation.

For example, if your string is in $arrayStr and you want to create an array $myArray from that:

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

Note that if $arrayStr is not entirely controller by you from generation to conversion, it is highly risky since anything in it would be evaluated.

A better solution to exchange a whole array between parts of your application would be to serialize() it beforehand to create a string representation of your array/object which can be stored, and then unserialize it when you need it in its original form.

1
  • can you explain me how works (in this case) the serialize() function? Feb 18, 2014 at 10:24
1

There's two main methods I can think of.

One, is to use some sort of complex pattern matching loop using preg_match. You can split the string into each line by using explode and \n to convert it into an array (of each line), loop through each row, and use pattern matching (regex) to figure out what to do with it.

The second method is incredibly dangerous, so I highly advise you to not use it, but php has it's own eval function which would let PHP interpret the string as actual PHP.

You would need to append a variable assignment inside the string e.g.

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

And then

eval($arrayString);

However, you really don't want to be doing this if you can. From the PHP docs:

Caution

The eval() language construct is very dangerous because it allows execution of arbitrary PHP code. Its use thus is discouraged. If you have carefully verified that there is no other option than to use this construct, pay special attention not to pass any user provided data into it without properly validating it beforehand.

1
  • your solution generate string()= array(.. is the same of the other function, this is my problem.. the eval not work fine.. but thank you so much! Feb 18, 2014 at 10:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.