/    Sign up×
Community /Pin to ProfileBookmark

I’m trying to use file() to read a file that looks like:

varable1 = this is the item
variable2 = this is another itm
it is on two lines
var3 = This is anotherone but they keep going on

I want to load the variables into a multi-array
I tried using parse_ini but its not working
Maybe I have to use file()?
Thanks

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@NogDogApr 04.2006 — variable2 = this is another itm

it is on two lines[/quote]

The multi-line entries are going to be a problem.

However, if you write the file as valid PHP code within <?php ... ?> tags, then you can just use [b]include[/b] and you won't have to do any parsing.
Copy linkTweet thisAlerts:
@BrutusUnixauthorApr 04.2006 — I'm not writing the file or I would, I'm trying to load a Makefile from the FreeBSD Port tree example:
[CODE]
PORTNAME= dnscheck
PORTVERSION= 1.2
CATEGORIES= dns
MASTER_SITES= http://www.netleader.com.au/projects/dnscheck/download/

MAINTAINER= [email protected]
COMMENT= DNS zone sanity checker
[/CODE]


I'm looking to load the above into a multi-array and then dump to a database

Anyhelp would be great
Copy linkTweet thisAlerts:
@NogDogApr 04.2006 — [code=php]
<?php
header("Content-type: text/plain");
$contents = file("data.txt");
foreach($contents as $line)
{
$line = trim($line);
if($line != "") // some sort of content on this line
{
if(preg_match('/^S+=/', $line)) // new item
{
list($key, $value) = explode('=', $line);
$data[$key] = trim($value);
}
elseif(!empty($key))
{
$data[$key] .= " $line";
}
else
{
die("Data file format problem");
}
}
}
print_r($data);
?>
[/code]

Here's the file I tested it with:
<i>
</i>PORTNAME= dnscheck
PORTVERSION= 1.2
CATEGORIES= dns
MASTER_SITES= http://www.netleader.com.au/projects/dnscheck/download/

MAINTAINER= [email protected]
COMMENT= DNS zone sanity checker
MULTILINE= This is line 1
This is line 2
This is line 3
LAST_ITEM= This is the last item

And this is the result:
<i>
</i>Array
(
[PORTNAME] =&gt; dnscheck
[PORTVERSION] =&gt; 1.2
[CATEGORIES] =&gt; dns
[MASTER_SITES] =&gt; http://www.netleader.com.au/projects/dnscheck/download/
[MAINTAINER] =&gt; [email protected]
[COMMENT] =&gt; DNS zone sanity checker
[MULTILINE] =&gt; This is line 1 This is line 2 This is line 3
[LAST_ITEM] =&gt; This is the last item
)
Copy linkTweet thisAlerts:
@BrutusUnixauthorApr 04.2006 — Thanks!!!
×

Success!

Help @BrutusUnix spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...