View Single Post
  #4 (permalink)  
Old 07-14-2006, 10:14 PM
curtiss's Avatar
curtiss curtiss is offline
Moderator
 
Join Date: May 2003
Posts: 1,467
Okay, so I've got a new, related problem.

I'm now trying to parse a Perl file that contains a bunch of variables. Unfortunately, the file contains variables that are defined in the following four different ways:

Code:
$var_name = 1;
$var_name = "text";
$var_name = q^Lots of text^;
$var_name = qq~Even more text~;
I'm not sure how to parse this file. I orginally thought about simply wrapping the whole string with <?php ?> tags, and parsing it that way, but PHP won't recognize the variables defined with the q^. I then tried using a preg_match_all regex (which I think is the best way to go), but unfortunately, I don't know how to use the "|" identifier when one of your possibilities is nothing.

For instance, here is one of the many configurations I've tried:
Code:
preg_match_all('/\$(.*) = ("|q\^|qq~|)(.*)(|\^|"|~)\;(.*)/',$ysetting,$out, PREG_PATTERN_ORDER);
That almost gets me what I want, but, unfortunately, it is not taking the terminating quote, ^, etc. off the end of the variable definition on some of my variables. Even stranger, on one or two of the variable definitions, it actually cuts out my variable definition altogether.

Here is the full code I'm currently using to get this info:
Code:
$ysettings = file("$varsdir/Settings.pl") OR die("Could not access Settings.pl");

foreach($ysettings as $ysetting) {
	chomp($ysetting);
	preg_match_all('/\$(.*) = ("|q\^|qq~|)(.*)(|\^|"|~)\;(.*)/',$ysetting,$out, PREG_PATTERN_ORDER);
	if($out[0][0] == "") {
		next($ysettings);
	}
	else {
		$tag = "yabb_".$out[1][0];
		$value = $out[3][0];
		$message .= "$tag = $value<br />";
		$$tag = $value;
	}
}
You can see the output of the variable definitions by going to http://dchelp.net/yabblog/

I'm working on a PHP login script that uses a bunch of information from a user's YaBB forum in order to allow them to log users into their boards through their web sites. The output of the entire login script is what you'll see when you click on the link above. I've got temporary debug information in there right now that shows you the output from the block of code I posted above (the information that's added to "$message" during the foreach loop).

EDIT - I've also attached the Perl file that I'm trying to parse with this code, in case you need to see that. I've saved it as a txt file, but, on the user's server, it will actually be a PL file.
Attached Files
File Type: txt Settings.txt (12.3 KB, 245 views)
__________________
I hate Internet Explorer! Anyone with me?

Last edited by curtiss; 07-14-2006 at 10:19 PM.
Reply With Quote