Jump to content

Recommended Posts

Okay, so I'm trying to attempt to parse a configuration file for a program I'm using.  Basically I'm going to end up parsing it and allowing a user to edit it, write it and download the finished file.

 

Each line represents an item that has directives within.  The directives are encased in []'s, they then have an operator (i.e. >=, ==, <=, etc) followed by the value for that operator.  Seems easy at first but some of them have hard balls thrown in there, such as directives located within brackets to indicate a group.

 

Here are some examples:

[Type]==Boots&&[Quality]==rare#[FRW]>=30&&(([FHR]==10&&[Dexterity]>=9)||[itemMagicBonus]>=25||[itemGoldBonus]>=80)&&([ColdResist]+[LightResist]+[FireResist]+[PoisonResist]>=90||([MaxMana]>=35&&[ColdResist]+[LightResist]+[FireResist]+[PoisonResist]>=70))

 

[Type]==Gloves&&[Quality]==rare#[iAS]==20&&[bowandCrossbowSkillTab]==2&&[LifeLeech]>=3&&[ManaLeech]>=3

 

The first one is one of the harder ones, the second one I already can do for the most part.  Directives that have + signs in between them are linked instead of using && operators to define all of them.

 

This is the code I have currently:

$handle = @fopen("Everything.NIP", "r");

if ($handle) {
   while (!feof($handle)) {
       $lines[] = fgets($handle, 4096);
   }
   fclose($handle);
}
$i=1;
foreach($lines as $x) {
$x = str_replace(' ', '', $x);
$Line = explode("#", $x);

$Base = $Line[0];
$Extra = $Line[1];


// Calc base stats
echo "<br>";
preg_match_all('/\[(.*?)\](.*?)(.*?)/si', $Base, $nvhost_matches);
echo "$i Base Stats:<br>";
	foreach($nvhost_matches[3] as $m) {
		echo "$m<br>";
	}

preg_match_all('/\[(.*?)\](.*?)(.*?)/si', $Extra, $nvhost_matches);
echo "$i Extra Stats:<br>";
	foreach($nvhost_matches[3] as $m) {
		echo "$m<br>";
	}
echo "$x<br><br>";
$i++;

}

 

It doesn't even work right now since I extract all the spaces in the string.  The spacing is screwed up in the config file so I figured it'd be easier to strip it and parse everything without spaces.

 

Any help at all would be appreciated.

Link to comment
https://forums.phpfreaks.com/topic/197777-no-idea-what-im-doing/
Share on other sites

Alright let me focus on my main problem since I probably was too vague with my question.

 

If I have a string like this

 

[Type]==Boots&&[Quality]==rare#[FRW]>=30&&(([FHR]==10&&[Dexterity]>=9)||[itemMagicBonus]>=25||[itemGoldBonus]>=80)

 

The first three directives with operators and values I can pull out so it would look like this:

 

Base:

Type == Boots

Quality == rare

Extra:

FRW >= 30

 

Now as it gets to the enclosed groups, that's where I am stuck.

 

I need

(([FHR]==10&&[Dexterity]>=9)||[itemMagicBonus]>=25||[itemGoldBonus]>=80)

 

To be:

Group 1:

    Subgroup 1:

    FHR == 10

    Dexterity >= 9

and (substituting for || operator)

ItemMagicBonus >= 25

and

ItemGoldBonus >= 80

 

I'm trying to get the full group which is the overall statement enclosed in ()s and then subgroups also enclosed in ()s.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.