Asheeown Posted April 6, 2010 Share Posted April 6, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/197777-no-idea-what-im-doing/ Share on other sites More sharing options...
Asheeown Posted April 7, 2010 Author Share Posted April 7, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/197777-no-idea-what-im-doing/#findComment-1038458 Share on other sites More sharing options...
Asheeown Posted April 7, 2010 Author Share Posted April 7, 2010 If any extra explanation or examples are needed just let me know. Quote Link to comment https://forums.phpfreaks.com/topic/197777-no-idea-what-im-doing/#findComment-1038674 Share on other sites More sharing options...
cags Posted April 8, 2010 Share Posted April 8, 2010 It would very much depend on how generic that pattern is. Using your specific example you could grab the part in the brackets for processing using something like... '#^[^(]*\((.*))$#' Quote Link to comment https://forums.phpfreaks.com/topic/197777-no-idea-what-im-doing/#findComment-1038849 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.