在php中解析文件到数组

时间:2010-10-29 22:14:00

标签: php arrays

请帮助我,我的问题是:

在一个.txt文件中我有

rpgoCPpref = {
 ["enabled"] = true,
 ["button"] = true,
 ["debug"] = false,
 ["questsfull"] = false,
 ["tooltipshtml"] = true,
 ["tooltip"] = true,
 ["verbose"] = false,
 ["scan"] = {
  ["inventory"] = true,
  ["talents"] = true,
  ["glyphs"] = true,
  ["honor"] = true,
  ["reputation"] = true,
  ["spells"] = true,
  ["pet"] = true,
  ["equipment"] = true,
  ["currency"] = true,
  ["companions"] = true,
  ["professions"] = true,
  ["mail"] = true,
  ["skills"] = true,
  ["quests"] = true,
  ["bank"] = true,
 },
 ["ver"] = 30000,
 ["fixicon"] = true,
 ["talentsfull"] = true,
 ["fixtooltip"] = true,
 ["fixcolor"] = true,
 ["lite"] = true,
 ["reagentfull"] = true,
 ["fixquantity"] = true,
}

谁是PHP中的数组转换或解析的形式?为你帮助thx

3 个答案:

答案 0 :(得分:0)

你必须阅读每一行并手动解释和构造数组!

答案 1 :(得分:0)

它是如何存储的?看起来您已将print_r调用的输出发送到文件。如果可能,您应该使用serialize命令将数组存储到文件:http://php.net/manual/en/function.serialize.php,然后您可以在以后反序列化内容:http://www.php.net/manual/en/function.unserialize.php

更多信息:http://www.php.net/manual/en/language.oop5.serialization.php

答案 2 :(得分:0)

假设您从不允许其他人将新代码注入此文件,您可以执行以下操作将其转换为常规PHP数组并通过eval传递:

$str = file_get_contents($your_file);

$str = preg_replace('/(["\w]+) = {/', '$\1 = array(', $str);
$str = preg_replace('/\[(["\w]+)\] = {/', '\1 => array(', $str);
$str = preg_replace('/\[(["\w]+)\] = (.+),/', '\1 => \2,', $str);
$str = preg_replace('/}/', ')', $str);

eval($str);

var_dump($rpgoCPpref);

这是一个非常好的想法,你可以废弃它,然后用serialized形式将数组写回来。