我在 CodeIgniter(v2.x)中有一个PHP应用程序,其中有一个配置文件,该文件具有相当标准的PHP格式的值,例如:
$config['key1'] = 'value1';
$config['key2'] = 'value2';
我正在编写一个Perl程序,该程序每晚运行一次,以将信息解析到数据库中供PHP使用/显示,我能否让Perl程序从PHP读取配置值?
我尝试进行PHP::Include module
,但是生成的Perl值似乎是空白的(使用DEBUG)模块。我也尝试过Config::IniFiles module
,但是我的源没有配置为ini
文件。
谢谢
答案 0 :(得分:3)
如果它是一个像这样的简单配置文件...
<?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/'
}
};
答案 1 :(得分:0)
是的,您可以将配置数组转换为与语言无关的格式,例如json:
$json = json_encode($this->config->config);
现在您已经有了json格式的配置数组,您可以将其保存到文件中,或者直接从程序中通过ajax请求检索它,或者执行任何您想使用的操作。