$str = 'answers[0] = "Love it"; answers[1] = "Hate it."; answers[2] = "Doesnt care"; answers[3] = "Like it";'
如何将其转换为数组?
采用
preg_split
只选择值并将其放入数组
例子:
$keywords = preg_split("/[\s,]+/", $str);
只是改变模式!
检查文档: http: //php.net/manual/en/function.preg-split.php
$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];
}
$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);
如果您可以在分号 ( ;
) 后添加新行,请查看该parse_ini_string()
函数。
$str = str_replace(';', "\n", $str); // <- add them in the source if you can
$array = parse_ini_string($str);
像这样的东西?
<?php
$str = '$answers[0] = "Love it"; $answers[1] = "Hate it."; $answers[2] = "Doesnt care"; $answers[3] = "Like it";';
eval($str);
print_r($answers);
?>
$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);