0

How to convert a string $string = 'array(array("a"=>"content"))'; (<-- this is string) into array like this:

array(1) { [0]=> array(1) { ["a"]=> string(7) "content" } }

I am going to build a function to run serialize online:

Input: $string = 'array(array(1))'; echo serialize($string);

Result: a:1:{i:0;a:1:{i:0;i:1;}} NOT s:16:"array(array(1)))";

You call: $returnValue = serialize('array(array(1))');

3
  • 2
    My question would be why you have such a string in the first place? Commented Aug 10, 2013 at 14:24
  • 2
    Please provide more context. You had two people answer eval when you already knew that eval is insecure and not a solution. Don't waste people's time suggesting things that you rule out right from the start. Note the section "Be specific" in stackoverflow.com/questions/ask-advice Commented Aug 10, 2013 at 14:48
  • @Gordon: I updated more info above. Commented Aug 10, 2013 at 15:11

2 Answers 2

4

You /could/ use eval():

$string = 'array(array("a"=>"content"))';
eval("\$array = $string;");
print_r($array);

Output:

Array
(
    [0] => Array
        (
            [a] => content
        )

)

But if you're accepting user inputs, you should not use eval().

Also, consider using json_encode() / json_decode() instead.

Alternatively, to store arrays in strings, you could use serialize and unserialize.

Sign up to request clarification or add additional context in comments.

6 Comments

if $string get data from $_POST like: $string = $_POST['string']; Like this: $string = 'array(array("a"=>"content")); die("HACK: add more functions before this")'; eval("\$array = $string;"); print_r($array);
I still hack if using eval() $string = 'array(array("a"=>"content")); die("HACK: add more functions before this")'; eval("\$array = $string;"); print_r($array); Output: HACK: add more functions before this
@Truongnq If you are gettig the string from outside you should write a parser. It might be better to agree on something simpler, like JSON.
@Truongnq: As I mentioned in the answer, you shouldn't use eval if it involves accepting input from user. Would've been kind of nice if you'd mentioned in your question that you don't want to use eval. Anyway I've suggested alternatives - you might want to check it out. :-)
@AmalMurali: I can see your updated answer. Can you explain more. Thanks!
|
0

eval? check http://php.net/manual/en/function.eval.php

take note on the Caution the manual shows...

2 Comments

eval is not security when I public this kind of function only. I tried, and hacked it when use eval.
I tried: $reg_exp = "/array(*.*)/"; $subject ="array(array(1=>'123'))"; $result = preg_split($reg_exp,$subject); var_dump($result); Output: array(2) { [0]=> string(0) "" [1]=> string(0) "" } but this is NOT correct. Please help!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.