#1 (permalink)  
Old 07-12-2006, 05:47 PM
curtiss's Avatar
Moderator
 
Join Date: May 2003
Posts: 1,445
Convert Perl Regex to PHP

Is there any chance someone might be able to convert the following regex from Perl to PHP for me? I don't have a very firm grasp on regexes in Perl or PHP, so I'm not sure what I would use to achieve this same goal in PHP.

Basically, what the following code does is go through the value of a string, and pulls two separate variables out of that string (those variables are represented in the regex by "(.*?)"). Then, we can store those two things as separate variables.

Code:
				$setting =~ s/\'(.*?)\'\,\"(.*?)\"//ig;
				my $tag   = $1;
				my $value = $2;
I could probably use something like:
Code:
$setting = preg_replace('/\'\,"/','|',$setting);
$setting = preg_replace('/\'/','',$setting);
$setting = preg_replace('/"/','',$setting);
$setting = preg_split('/|/',$setting);
$tag = $setting[0];
$value = $setting[1];
but that seems like an awful lot of code to accomplish three lines of Perl.
__________________
I hate Internet Explorer! Anyone with me?
Reply With Quote

  #2 (permalink)  
Old 07-13-2006, 07:59 AM
Till's Avatar
Administrator
 
Join Date: Jan 2002
Location: Berlin, Germany
Posts: 1,453
What you want is PHP's preg_match_all().
Reply With Quote
  #3 (permalink)  
Old 07-13-2006, 09:09 AM
curtiss's Avatar
Moderator
 
Join Date: May 2003
Posts: 1,445
Huzzah!

It worked. Thank you. It took quite a bit of work to figure out exactly how that function works, but I did finally figure it out, and I've got it working nicely.

Thank you.

Just so you know, this is the code I finally ended up using:
Code:
				preg_match_all('/\'(.*)\'\,\"(.*)\"/', $setting, $settings, PREG_PATTERN_ORDER);
				$tag = "yabb_".$settings[1][0];
				$value = $settings[2][0];
				$$tag = $value;
__________________
I hate Internet Explorer! Anyone with me?
Reply With Quote
  #4 (permalink)  
Old 07-14-2006, 10:14 PM
curtiss's Avatar
Moderator
 
Join Date: May 2003
Posts: 1,445
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, 236 views)
__________________
I hate Internet Explorer! Anyone with me?

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


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with writing file in perl gugabaga Programming and Scripting 1 06-27-2006 09:17 AM
PHP mail problems khelter Programming and Scripting 1 04-17-2006 08:36 AM
PHP and <a name> Leprakawn Programming and Scripting 6 02-02-2006 04:28 PM
Returning Without Going "Back" in Perl? curtiss Programming and Scripting 5 01-30-2006 04:39 PM
PERL Convert Date Deadeye Programming and Scripting 1 10-17-2005 07:45 PM


All times are GMT -5. The time now is 04:21 PM.

 
Bitrix
Clicky Web Analytics
CloudContacts
Maxtango


Subscribe to our feed | add to myYahoo!

Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.2.0
© 1997-2007 HTMLCenter