Author Topic: Help  (Read 571 times)

0 Members and 1 Guest are viewing this topic.

Offline drisateTopic starter

  • Devotee
  • Posts: 747
    • View Profile
Help
« on: February 07, 2010, 11:42:13 PM »
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 ---------
« Last Edit: February 07, 2010, 11:51:32 PM by drisate »
www.VersatileBB.com - Open Source Board
Easy to use and modify board for your website. A very good alt to all those to much  popular boards

Offline MadTechie

  • Guru
  • Freak!
  • *
  • Posts: 9,374
  • Gender: Male
  • I try to F1
    • View Profile
Re: Help
« Reply #1 on: February 07, 2010, 11:49:35 PM »
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];
« Last Edit: February 07, 2010, 11:52:42 PM by MadTechie »
Computers are good at following instructions, but not at reading your mind.
The quality of a response, is usually directly related to the quality of the question. ©2009 mjdamato
I dunno about that.  A regular expression has a 0% chance of touching my penis.
the code is professionally made up but not working
Remember to Click Solved, how to ask questions - the smart way

Offline drisateTopic starter

  • Devotee
  • Posts: 747
    • View Profile
Re: Help
« Reply #2 on: February 07, 2010, 11:59:10 PM »
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 ---------
« Last Edit: February 08, 2010, 12:00:03 AM by drisate »
www.VersatileBB.com - Open Source Board
Easy to use and modify board for your website. A very good alt to all those to much  popular boards

Offline MadTechie

  • Guru
  • Freak!
  • *
  • Posts: 9,374
  • Gender: Male
  • I try to F1
    • View Profile
Re: Help
« Reply #3 on: February 08, 2010, 12:04:07 AM »
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
}
Computers are good at following instructions, but not at reading your mind.
The quality of a response, is usually directly related to the quality of the question. ©2009 mjdamato
I dunno about that.  A regular expression has a 0% chance of touching my penis.
the code is professionally made up but not working
Remember to Click Solved, how to ask questions - the smart way

Offline drisateTopic starter

  • Devotee
  • Posts: 747
    • View Profile
Re: Help
« Reply #4 on: February 08, 2010, 12:09:52 AM »
Ok thanks. 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
www.VersatileBB.com - Open Source Board
Easy to use and modify board for your website. A very good alt to all those to much  popular boards

Offline MadTechie

  • Guru
  • Freak!
  • *
  • Posts: 9,374
  • Gender: Male
  • I try to F1
    • View Profile
Re: Help
« Reply #5 on: February 08, 2010, 12:21:13 AM »
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
Quote
Small Cargo  (5)-> Light Fighter (556)<BR />
Cruiser  (3)-> Colony Ship (1)<BR />
Espionage Probe  (10)-> Solar Satellite (50)<BR />
Computers are good at following instructions, but not at reading your mind.
The quality of a response, is usually directly related to the quality of the question. ©2009 mjdamato
I dunno about that.  A regular expression has a 0% chance of touching my penis.
the code is professionally made up but not working
Remember to Click Solved, how to ask questions - the smart way

Offline drisateTopic starter

  • Devotee
  • Posts: 747
    • View Profile
Re: Help
« Reply #6 on: February 08, 2010, 12:27:40 AM »
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 =
www.VersatileBB.com - Open Source Board
Easy to use and modify board for your website. A very good alt to all those to much  popular boards

Offline MadTechie

  • Guru
  • Freak!
  • *
  • Posts: 9,374
  • Gender: Male
  • I try to F1
    • View Profile
Re: Help
« Reply #7 on: February 08, 2010, 04:02:28 AM »
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)) {
Computers are good at following instructions, but not at reading your mind.
The quality of a response, is usually directly related to the quality of the question. ©2009 mjdamato
I dunno about that.  A regular expression has a 0% chance of touching my penis.
the code is professionally made up but not working
Remember to Click Solved, how to ask questions - the smart way

Offline drisateTopic starter

  • Devotee
  • Posts: 747
    • View Profile
Re: Help
« Reply #8 on: February 08, 2010, 09:02:54 AM »
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
www.VersatileBB.com - Open Source Board
Easy to use and modify board for your website. A very good alt to all those to much  popular boards

Offline MadTechie

  • Guru
  • Freak!
  • *
  • Posts: 9,374
  • Gender: Male
  • I try to F1
    • View Profile
Re: Help
« Reply #9 on: February 08, 2010, 11:54:02 AM »
this should do it
Code: [Select]
^([a-z -]+):\s+([\d\.]+)\s+([a-z -]+):\s+([\d\.]+)
Computers are good at following instructions, but not at reading your mind.
The quality of a response, is usually directly related to the quality of the question. ©2009 mjdamato
I dunno about that.  A regular expression has a 0% chance of touching my penis.
the code is professionally made up but not working
Remember to Click Solved, how to ask questions - the smart way