using REGEX to read database-file
Posted: Sat Aug 11, 2007 8:10 am
I am stuck at trying to read a database-file that is quite simple. I just can't get it to work.
The structure of the file is
for example:
I would like to get an array with name-value pairs.
I found a piece of PERL code that does precisely that.
I translated that to php:
Is it possible to copy the regex that literal from perl to php (I only took the 'g' away)? If no, what should I change?
For some reason, the function keeps on picking up the first var-name/var-value pair endlessly. If I use preg_match_all instead, $value and $key are arrays, and it still keeps on looping endlessly.
The structure of the file is
Code: Select all
\nVAR-NAME: VAR-VALUE
\tVAR-VALUE
\tVAR-VALUECode: Select all
var1: tttttt rlkvf; ercm e,dro: lwmeurdiw
dkkdkdkdkdkadk;a
;lr er,fo4 o354,fp3,f5p4 of,more
var2: kdl;wskokfm3[p
var3: kf;l'dsv,eçrf
lse.cr dlrltkf;l
etc.I found a piece of PERL code that does precisely that.
Code: Select all
sub ParseData {
my $data = shift;
my %result;
while ($data =~ /(\S+?): (.*?)(?=\n[^ \t]|\Z)/sg) {
my ($key, $value) = ($1, $2);
$value =~ s/\n\t/\n/g;
$result{$key} = $value;
}
return %result;
}Code: Select all
function ParseData ($data) {
while ( preg_match( "/(\S+?): (.*?)(?=\n[^ \t]|\Z)/s", $data, $match ) ) {
$key = $match[1];
$value = $match[2];
$value = preg_replace( "/\n\t/", '\n', $value );
$result[$key] = $value;
}
return $result;
}For some reason, the function keeps on picking up the first var-name/var-value pair endlessly. If I use preg_match_all instead, $value and $key are arrays, and it still keeps on looping endlessly.