我将下面提到的代码(实际上是配置)写入文件(config.php)中,我想将 config.php 中写入的内容写入另一个文件(check.php)
config.php中编写的代码:
<?php
$CONFIG = array (
'id' => 'asd5646asdas',
'dbtype' => 'mysql',
'version' => '5.0.12',
);
Run Code Online (Sandbox Code Playgroud)
check.php中编写的获取内容的代码是:
$config_file_path = $_SERVER['DOCUMENT_ROOT'] . 'config.php';
$config = file_get_contents($config_file_path);
Run Code Online (Sandbox Code Playgroud)
使用上面的代码,我将输出作为字符串,我想将其转换为数组。为此,我尝试了以下代码。
$config = substr($config, 24, -5); // to remove the php starting tag and other un-neccesary things
$config = str_replace("'","",$config);
$config_array = explode("=>", $config);
Run Code Online (Sandbox Code Playgroud)
使用上面的代码,我得到如下输出:
Array
(
[0] => id
[1] => asd5646asdas,
dbtype
[2] => mysql,
version
[3] => 5.0.12
)
Run Code Online (Sandbox Code Playgroud)
这是不正确的。
有什么办法可以将其转换为数组吗?我已经尝试过 serialize() 以及从 php 中的另一个页面访问数组中提到的内容,但没有成功。
对此的任何帮助将不胜感激。
你不需要file_get_contents:
require_once 'config.php'
var_dump($CONFIG);
Run Code Online (Sandbox Code Playgroud)