eval的替代品('$ array_test = array('。$ test。');')

I'm looking for an alternative to

$test = "1=>'msg_test1',3=>'msg_test2',9=>'msg_test3'";
eval('$array_test = array('.$test.');');

does anyone have an idea how i can make it to have an array in a secure way from a string?

Thanks in advance for your help!

You could parse it manually with explode() like so:

$test = "1=>'msg_test1,3=>'msg_test2,9=>'msg_test3'";

$array_test = array();
foreach(explode(',', substr($test, 0, -1)) as $row)
{
    $split = explode('=>\'', $row);
    $array_test[$split[0]] = $split[1];
}

var_dump($array_test);

Produces:

array (size=3)
  1 => string 'msg_test1' (length=9)
  3 => string 'msg_test2' (length=9)
  9 => string 'msg_test3' (length=9)

If I understand your question then you cshould format your string like this

$test = "value1,value2,value2";

Then proceed with and explode

$array_test = explode(',', $test);

your array

$array_test = array(
    '1' => 'value1'
    '2' => 'value2'
    '3' => 'value3'
);