$str = 'answers[0] = "Love it"; answers[1] = "Hate it."; answers[2] = "Doesnt care"; answers[3] = "Like it";'
如何将其转换为数组?
答案 0 :(得分:1)
使用
使preg_split
只选择值并将其放入数组
示例:
$keywords = preg_split("/[\s,]+/", $str);
只需改变模式!
查看文档:{{3}}
答案 1 :(得分:1)
$str = 'answers[0] = "Love it"; answers[1] = "Hate it."; answers[2] = "Doesnt care"; answers[3] = "Like it";';
$chunks = explode(';', $str);
for ($aa=0;$aa<count($chunks);$aa++)
{
$chunks1 = explode('=', $chunks[$aa]);
$myArray[]=$chunks1[1];
}
答案 2 :(得分:1)
$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);
答案 3 :(得分:1)
如果您可以在分号(;
)之后添加新行,请查看parse_ini_string()
函数。
$str = str_replace(';', "\n", $str); // <- add them in the source if you can
$array = parse_ini_string($str);
答案 4 :(得分:0)
这样的东西?
<?php
$str = '$answers[0] = "Love it"; $answers[1] = "Hate it."; $answers[2] = "Doesnt care"; $answers[3] = "Like it";';
eval($str);
print_r($answers);
?>
答案 5 :(得分:0)
$str = 'answers[0] = "Love it"; answers[1] = "Hate it."; answers[2] = "Doesnt care"; answers[3] = "Like it";';
preg_match_all('/"(.*?)"/', $str, $answers);
$answer = $answers[0];
print_r($answer);