Если это простой конфигурационный файл, такой как ...
<?php
/**
*
* service: /www/vhosts/x456/docs/config.php
* program: community one
* version: 1.01, 01/01/2009 01:01:01
*
**/
$config = array ();
$config['system_admin'] = 1;
$config['system_name'] = 'Community One';
$config['session_name'] = 'community';
$config['system_email'] = '@gmail.com';
$config['system_allow'] = '127.0.0.1';
$config['storage_type'] = 'file';
$config['host_name'] = '';
$config['service_path'] = '/';
$config['system_root'] = 'c:/services/www/vhosts/x456/';
$config['document_root'] = 'c:/services/www/vhosts/x456/docs/';
$config['database_name'] = 'community';
$config['database_host'] = 'localhost';
$config['database_user'] = 'admin';
$config['database_pass'] = '';
$config['database_type'] = 'mysqli';
$config['database_port'] = '3306';
$config['database_salt'] = '*^&$_';
$config['min_timeout'] = 180;
$config['max_timeout'] = 31557600;
$config['ssl_only'] = FALSE;
?>
Вы можете использовать этот старый скрипт, который у меня есть ...
use strict;
use warnings;
use Data::Dumper qw ( Dumper );
#error log location and name
my $log = 'C:/services/www/log/errors.txt';
# the hash container
my %data;
# the config file processor
sub configFile
{
# grab the config files location
my $file = shift;
# open the config file up
open ( CONFIG, '<', $file ) or logError ( 0, $file );
# read the config file, line by line
while ( my $line = <CONFIG> )
{
# lose the eol and any spaces found >> (^|$)
$line = trim ( $line );
# move to next line if we don't have a $var
next if $line !~ /^\$/;
# move to the next line if there is no
# (key = value) pair
my $find = index $line, '=';
# grab the array & key name(s) + clean them up
my $name = rtrim ( substr $line, 0, $find );
# grab the value + clean it up
my $value = ltrim ( substr $line, ( $find + 1 ) );
# final check, skip over $var(s) = array(), []
# process only $var['key'] = values!
next if ( my $pos = index $name, '[' ) == -1;
# set the (hash key) name = $(hash key)
my $hash = substr $name, 1, ( $pos - 1 );
# set the variables (key) name = $(hash key)[(key)]
my $key = substr $name, ( $pos + 2 ), -2;
# strip ('|'|;) from the variable(s) value
if ( $value =~ m/^'|^"/ )
{
$value = substr $value, 1, -2;
}
else
{
$value = substr $value, 0, -1;
}
# add the varaible to the data hash
$data{$hash}{$key} = $value;
}
# done, close it
close CONFIG;
}
sub logError ( )
{
my ( $type, $data ) = @_;
open ( ERRORS, '>>', $log );
print ERRORS $type . ", " . $data . "\n";
close ERRORS;
exit ( 0 );
}
sub ltrim
{
my $s = shift;
$s =~ s/^\s+//;
return $s
};
sub rtrim
{
my $s = shift;
$s =~ s/\s+$//;
return $s
};
sub trim
{
my $s = shift;
$s =~ s/^\s+|\s+$//g;
return $s
};
# the php config style file to process
configFile ( 'C:\\services\\www\\config.php' );
# just print the $data hash out so you
# can see what it returns
print Dumper \%data;
В результате будет ....
$VAR1 = {
'config' => {
'ssl_only' => 'FALSE',
'database_salt' => '*^&$_',
'system_allow' => '127.0.0.1',
'system_email' => '@gmail.com',
'max_timeout' => '31557600',
'database_type' => 'mysqli',
'database_pass' => '',
'storage_type' => 'file',
'database_user' => 'admin',
'min_timeout' => '180',
'system_admin' => '1',
'system_name' => 'Community One',
'system_root' => 'c:/services/www/vhosts/x456/',
'database_host' => 'localhost',
'database_port' => '3306',
'session_name' => 'community',
'service_path' => '/',
'database_name' => 'community',
'host_name' => '',
'document_root' => 'c:/services/www/vhosts/x456/docs/'
}
};