【发布时间】:2013-06-18 14:01:16
【问题描述】:
我将下面提到的代码(实际上是配置)写在一个文件(config.php)中,我想将 config.php 中写的内容放在另一个文件(check.php)中
config.php 中编写的代码:
<?php
$CONFIG = array (
'id' => 'asd5646asdas',
'dbtype' => 'mysql',
'version' => '5.0.12',
);
check.php中用来获取内容的代码是:
$config_file_path = $_SERVER['DOCUMENT_ROOT'] . 'config.php';
$config = file_get_contents($config_file_path);
使用上面的代码,我得到了一个字符串的输出,我想把它转换成一个数组。为此,我尝试了以下代码。
$config = substr($config, 24, -5); // to remove the php starting tag and other un-neccesary things
$config = str_replace("'","",$config);
$config_array = explode("=>", $config);
使用上面的代码,我得到如下输出:
Array
(
[0] => id
[1] => asd5646asdas,
dbtype
[2] => mysql,
version
[3] => 5.0.12
)
这是不正确的。
有什么办法可以把它转换成数组。我试过 serialize() 以及 accessing array from another page in php 中提到的,但没有成功。
对此的任何帮助将不胜感激。
【问题讨论】:
-
您可以包含该文件。如果你想解析这个数组,你可以查看question。答案中有一个custom php parser 和一个regex solution