Jump to content

Help


drisate

Recommended Posts

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: 8) 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 ---------

Link to comment
Share on other sites

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];

Link to comment
Share on other sites

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: 8) 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 ---------

Link to comment
Share on other sites

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
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 />

Link to comment
Share on other sites

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 =

 

Link to comment
Share on other sites

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.