2
$str = 'answers[0] = "Love it"; answers[1] = "Hate it."; answers[2] = "Doesnt care"; answers[3] = "Like it";'

如何将其转换为数组?

4

6 回答 6

1

采用

preg_split

只选择值并将其放入数组

例子:

$keywords = preg_split("/[\s,]+/", $str);

只是改变模式!

检查文档: http: //php.net/manual/en/function.preg-split.php

于 2013-09-26T10:22:30.933 回答
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];
}
于 2013-09-26T10:26:53.753 回答
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);
于 2013-09-26T10:30:50.403 回答
1

如果您可以在分号 ( ;) 后添加新行,请查看该parse_ini_string()函数。

$str   = str_replace(';', "\n", $str); // <- add them in the source if you can
$array = parse_ini_string($str);
于 2013-09-26T10:43:14.583 回答
0

像这样的东西?

<?php
$str = '$answers[0] = "Love it"; $answers[1] = "Hate it."; $answers[2] = "Doesnt care"; $answers[3] = "Like it";';
eval($str);
print_r($answers);

?>
于 2013-09-26T10:18:36.927 回答
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);
于 2013-09-26T10:37:11.497 回答