drisate Posted February 8, 2010 Share Posted February 8, 2010 Hey guys i need help extracting into usable vars each read part of the folowing texte. Resources at Blant Topia [2:464:4] (Player 'nemrack') on 02-08 05:09:39 Metal: 171.028 Crystal: 83.759 Deuterium: 222.907 Energy: 4.275 Activity Activity Activity means that the scanned player has been active on that planet or another player had fleet contact with the planet you scanned. Your espionage does not show abnormalities in the atmosphere of the planet. There appears to have been no activity on the planet within the last hour. fleets Small Cargo 5 Light Fighter 556 Cruiser 3 Colony Ship 1 Espionage Probe 10 Solar Satellite 50 Defense Rocket Launcher 600 Light Laser 100 Heavy Laser 5 Gauss Cannon 3 Ion Cannon 30 Small Shield Dome 1 Anti-Ballistic Missiles 1 Chance of counter-espionage: 15 % Attack The idea is creating a small texte box where the player will copy in the texte above after what it will be changed into a Board freindly bbcode format. I made a loop that explode the line like this ------- DEBUG Output --------- Line: 0) Resources at Blant Topia [2:464:4] (Player 'nemrack') on 02-08 05:09:39 Line: 1) Metal: 171.028 Crystal: 83.759 Line: 2) Deuterium: 222.907 Energy: 4.275 Line: 4) Activity Line: 5) Activity Line: 6) Activity means that the scanned player has been active on that planet or another player had fleet contact with the planet you scanned. Line: 7) Your espionage does not show abnormalities in the atmosphere of the planet. There appears to have been no activity on the planet within the last hour. Line: fleets Line: 9) Small Cargo 5 Light Fighter 556 Line: 10) Cruiser 3 Colony Ship 1 Line: 11) Espionage Probe 10 Solar Satellite 50 Line: 12) Defense Line: 13) Rocket Launcher 600 Light Laser 100 Line: 14) Heavy Laser 5 Gauss Cannon 3 Line: 15) Ion Cannon 30 Small Shield Dome 1 Line: 16) Anti-Ballistic Missiles 1 Line: 17) Chance of counter-espionage: 15 % Line: 18) Attack ------- DEBUG Output --------- Quote Link to comment Share on other sites More sharing options...
MadTechie Posted February 8, 2010 Share Posted February 8, 2010 try this preg_match('/^([a-z -]+)(\d+)(?:[ ]+([a-z -]+)(\d+))?$/im', $html, $regs); $Weapon1 = $regs[1]; $Power1 = $regs[2]; $Weapon2 = $regs[3]; $Power1 = $regs[4]; //counter Percent preg_match('/^Chance of counter.*?:\s+(\d+)\s+%\s+$/im', $html, $regs); $counter= $regs[1]; Quote Link to comment Share on other sites More sharing options...
drisate Posted February 8, 2010 Author Share Posted February 8, 2010 Nope ... this si what i have so fare. You code returns nothing <?php // first we check if the form has been posted if ($_POST[str]){ echo "------- DEBUG Output ---------<br><br>"; // We explode each lines $str = explode("\r\n",$_POST["str"]); $linecount = count($str); // Number of lines // We loop the lines $i=0; while($i<=$linecount){ // We filter out empty lines $line = str_replace (' ', '', $str[$i]); if ($line!=""){ preg_match('/^([a-z -]+)(\d+)(?:[ ]+([a-z -]+)(\d+))?$/im', $str[$i], $regs); $Weapon1 = $regs[1]; $Power1 = $regs[2]; $Weapon2 = $regs[3]; $Power2 = $regs[4]; // Line is not empty echo "Line: $i) ".$str[$i]."<br>"; } $i++; } echo "<br><br>Weapon1 = $Weapon1<br>Power1 = $Power1<br>Weapon2 = $Weapon2<br>Power2 = $Power2<br>"; echo "<br>------- DEBUG Output ---------"; }else{ print ('<form action="index.php?mod=esp" method="POST"> <p><textarea rows="16" name="str" cols="59"></textarea></p><p><input type="submit" value="Submit" name="B1"></p> </form>'); } ?> The above returns: ------- DEBUG Output --------- Line: 0) Resources at Blant Topia [2:464:4] (Player 'nemrack') on 02-08 05:09:39 Line: 1) Metal: 171.028 Crystal: 83.759 Line: 2) Deuterium: 222.907 Energy: 4.275 Line: 4) Activity Line: 5) Activity Line: 6) Activity means that the scanned player has been active on that planet or another player had fleet contact with the planet you scanned. Line: 7) Your espionage does not show abnormalities in the atmosphere of the planet. There appears to have been no activity on the planet within the last hour. Line: fleets Line: 9) Small Cargo 5 Light Fighter 556 Line: 10) Cruiser 3 Colony Ship 1 Line: 11) Espionage Probe 10 Solar Satellite 50 Line: 12) Defense Line: 13) Rocket Launcher 600 Light Laser 100 Line: 14) Heavy Laser 5 Gauss Cannon 3 Line: 15) Ion Cannon 30 Small Shield Dome 1 Line: 16) Anti-Ballistic Missiles 1 Line: 17) Chance of counter-espionage: 15 % Line: 18) Attack Weapon1 = Power1 = Weapon2 = Power2 = ------- DEBUG Output --------- Quote Link to comment Share on other sites More sharing options...
MadTechie Posted February 8, 2010 Share Posted February 8, 2010 If your breaking it up by line then, you don't need the start and end line tags So try this if (preg_match('/([a-z -]+)(\d+)(?:[ ]+([a-z -]+)(\d+))?/im', $str[$i], $regs)) { $Weapon1 = $regs[1]; $Power1 = $regs[2]; $Weapon2 = $regs[3]; $Power2 = $regs[4]; echo "$Weapon1 ($Power1)-> $Weapon2($Power2)<BR />\n"; //debug } Quote Link to comment Share on other sites More sharing options...
drisate Posted February 8, 2010 Author Share Posted February 8, 2010 Ok thx. I did this: if (preg_match('/([a-z -]+)(\d+)(?:[ ]+([a-z -]+)(\d+))?/im', $str[$i], $regs1)) { $Weapon1 = $regs1[1]; $Power1 = $regs1[2]; $Weapon2 = $regs1[3]; $Power2 = $regs1[4]; } if (preg_match('/^Chance of counter.*?:\s+(\d+)\s+%\s+$/im', $str[$i], $regs2)){ $counter= $regs2[1]; } And now i get Weapon1 = Power1 = 15 Weapon2 = Power2 = counter = 15 Quote Link to comment Share on other sites More sharing options...
MadTechie Posted February 8, 2010 Share Posted February 8, 2010 Humm, Works fine here I just tested it $data = 'Small Cargo 5 Light Fighter 556 Cruiser 3 Colony Ship 1 Espionage Probe 10 Solar Satellite 50'; $str = explode("\n",$data); foreach($str as $s){ if (preg_match('/([a-z -]+)(\d+)(?:[ ]+([a-z -]+)(\d+))?/im', $s, $regs)) { $Weapon1 = $regs[1]; $Power1 = $regs[2]; $Weapon2 = $regs[3]; $Power2 = $regs[4]; echo "$Weapon1 ($Power1)-> $Weapon2($Power2)<BR />\n"; //debug } } returns Small Cargo (5)-> Light Fighter (556)<BR /> Cruiser (3)-> Colony Ship (1)<BR /> Espionage Probe (10)-> Solar Satellite (50)<BR /> Quote Link to comment Share on other sites More sharing options...
drisate Posted February 8, 2010 Author Share Posted February 8, 2010 I think that preg_match is matching something else ... i tryed this : if (!$rexex1){ if (preg_match('/([a-z -]+)(\d+)(?:[ ]+([a-z -]+)(\d+))?/im', $str[$i], $regs1)) { $Weapon1 = $regs1[1]; $Power1 = $regs1[2]; $Weapon2 = $regs1[3]; $Power2 = $regs1[4]; $rexex1 = "used"; } } Just to make sure the preg_match is used just one time and it returned: Weapon1 = on Power1 = 02 Weapon2 = Power2 = Quote Link to comment Share on other sites More sharing options...
MadTechie Posted February 8, 2010 Share Posted February 8, 2010 if your testing every line add ^ to the start of the regex if (preg_match('/^([a-z -]+)(\d+)(?:[ ]+([a-z -]+)(\d+))?/im', $str[$i], $regs1)) { Quote Link to comment Share on other sites More sharing options...
drisate Posted February 8, 2010 Author Share Posted February 8, 2010 Ok i got it hehe how do i modify this regular expression /^([a-z -]+)(\d+)(?:[ ]+([a-z -]+)(\d+))?/im To make it pick up ? Metal: 171.028 Crystal: 83.759 Deuterium: 222.907 Energy: 4.275 The number may or may not contain a dot Quote Link to comment Share on other sites More sharing options...
MadTechie Posted February 8, 2010 Share Posted February 8, 2010 this should do it ^([a-z -]+):\s+([\d\.]+)\s+([a-z -]+):\s+([\d\.]+) Quote Link to comment 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.