PHP - 从ini文件获取一个日期(来自网址)

时间:2016-09-09 17:33:20

标签: php ini

我还是习惯了php。我试图从ini url文件中获取一个数据用于更改为变量。

存储ini文件的示例网址位于http://jla.justiceleague.com/idinfo.jl/MMH001(仅限示例,非工作网址)

文件看起来像:

 ver=1
 id=MMH001
 name=John Jonz
 origin=Mars
 org=Justice League
 web=mars.martianmanhunter.com

我想要做的是从ini文件中获取web url(又名" mars.martianmanhunter.com"),然后将其转换为php中的变量以用于以后的目的。

首先我们如何连接到ini文件。最重要的是,我们如何才能获得我需要获得的那一条信息?

1 个答案:

答案 0 :(得分:1)

您可以使用parse_ini_string()函数。像这样:

<?php

// Here you set the file/url
$url = 'http://jla.justiceleague.com/idinfo.jl/MMH001';

// Get the content of file/website
$webData = file_get_contents($url);

// Make $web and array of items in INI file
$web = parse_ini_string($webData);

// echo mars.martianmanhunter.com from the array
print_r($web['web']);

“$ web”包含的数组将如下所示:

Array
(
    [ver] => 1
    [id] => MMH001
    [name] => John Jonz
    [origin] => Mars
    [org] => Justice League
    [web] => mars.martianmanhunter.com
)

如果你不想每次都想要整个数组,只想在ini文件中使用“web”变量,那么就这样做:

$web = parse_ini_string($webData)['web'];
print_r($web);

现在“$ web”将只包含“mars.martianmanhunter.com”,并且不会获得“id”,“name”等内容。

希望这有帮助!