$str = 'answers[0] = "Love it"; answers[1] = "Hate it."; answers[2] = "Doesnt care"; answers[3] = "Like it";'
How can I convert it to an array?
$str = 'answers[0] = "Love it"; answers[1] = "Hate it."; answers[2] = "Doesnt care"; answers[3] = "Like it";'
How can I convert it to an array?
$str = 'answers[0] = "Love it"; answers[1] = "Hate it."; answers[2] = "Doesnt care"; answers[3] = "Like it";';
preg_match_all('/"(.*)";/U', $str, $matches);
$arr = $matches[1];
var_dump($arr);
Use
preg_split
to select just the values and put it in to an array
example:
$keywords = preg_split("/[\s,]+/", $str);
Just change the pattern!
check the documentation: http://php.net/manual/en/function.preg-split.php
If you can add new lines after the semicolons (;), take a look at the parse_ini_string() function.
$str = str_replace(';', "\n", $str); // <- add them in the source if you can
$array = parse_ini_string($str);
Something like this?
<?php
$str = '$answers[0] = "Love it"; $answers[1] = "Hate it."; $answers[2] = "Doesnt care"; $answers[3] = "Like it";';
eval($str);
print_r($answers);
?>
eval() if you don't have to. And there is always another way to achieve what you want to. Also, there is no $ signs in OP example string, so it's not an answer.