将文件解析为php中的数组 [英] parse file to array in php

查看:44
本文介绍了将文件解析为php中的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我,我的问题是:

please help me, my problem is:

我有一个.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,
}

w何是在PHP数组中转换或解析的形式?为您提供帮助

who is the form of convert or parse in array in php? for you help thx

推荐答案

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

Assuming you will never allow other people to inject new code into this file, you can do the following to turn it into a regular PHP array and pass it trough 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);

这对您来说是个好主意,可将其废弃并将数组写回到序列化表格。

It's a very good idea for you to scrap this and write the array back out in a serialized form instead.

这篇关于将文件解析为php中的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆