Page 1 of 1

using REGEX to read database-file

Posted: Sat Aug 11, 2007 8:10 am
by buzink
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

Code: Select all

\nVAR-NAME: VAR-VALUE
\tVAR-VALUE
\tVAR-VALUE
for example:

Code: 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 would like to get an array with name-value pairs.

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;
}
I translated that to php:

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;

}
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.

solved?

Posted: Sat Aug 11, 2007 10:15 am
by buzink
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


ok, solved it. Is there a more elegant way than doing it like this?:

Code: Select all

function ParseData ($data) {

	preg_match_all( "/(\S+?): (.*?)(?=\n[^ \t]|\Z)/s", $data, $match );

	$i = 0;

	foreach ( $match[1] as $key ) {

		$value = $match[2][$i];

		$value = preg_replace( "/\n\t/", '\n', $value );

		$result[$key] = $value;
		$i++;

	}

	return $result;

}

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon Aug 13, 2007 4:20 am
by stereofrog
maybe this

Code: Select all

$db = "
var1: abc
   def
   end
var2: one line
var3: yet
   another
   var
  ";

preg_replace_callback('/\n(\w+):(.*?)(?=\n\S|$)/s', '_parse', $db);

function _parse($x) {
	$GLOBALS['data'][$x[1]] = preg_replace('/\n\s+/', ' ', $x[2]);
}

print_r($data);