Parsing html form.

Reply

Join Date: Nov 2004
Posts: 35
Reputation: croft is an unknown quantity at this point 
Solved Threads: 0
croft's Avatar
croft croft is offline Offline
Light Poster

Parsing html form.

 
0
  #1
Jun 8th, 2005
Let me tell you this from the start =)

We play a game called eve.
In that game wheneve you kill someone you will recive an email wich i will post belove here for an idea how it looks like.

This game alsoo has an ingame browser html 1.4 compliant. not the biggest dream but it works =)

I want to create a form. only 1 form where you paste the entire content of the 'kill mail' and hit a submit button. That will be placed in mysql database. From there i want to be able to show the latest kills. top killer. top looser and so on on a webpage.

Can anyone help me get this thing going ?
Im pretty sure i can get the info from the database when its in there but i seam to have problems getting the information in to the database.

Here is 1 basic 'kill mail' i recived a couple of days agoo.
Would be much apriciated if someone could assist me with this.

  1. 2005.06.04 03:35:00
  2.  
  3. Victim: name of killed person here
  4. Alliance: Unknown
  5. Corporation: Storm Guard Elite
  6. Destroyed Type: Kestrel
  7. Solar System: H74-B0
  8. System Security Level: 0.0
  9.  
  10. Involved parties:
  11.  
  12. Name: name of killer here (laid the final blow)
  13. Security Status: 5.0
  14. Alliance: Firmus Ixion
  15. Corporation: Corp name here
  16. Ship Type: Megathron
  17. Weapon Type: 425mm Railgun II
  18.  
  19.  
  20.  
  21. Destroyed items:
  22.  
  23. Type: Standard Missile Launcher I (Fitted - High slot)
  24.  
  25. Type: Standard Missile Launcher I (Fitted - High slot)
  26.  
  27. Type: Warp Disruptor I (Fitted - Medium slot)
  28.  
  29. Type: Standard Missile Launcher I (Fitted - High slot)
  30.  
  31. Type: Sabretooth Light Missile I (Cargo)
  32. Quantity: 100
  33.  
  34. Type: Bloodclaw Light Missile I (Cargo)
  35. Quantity: 50
  36.  
  37. Type: Bloodclaw Light Missile I (Cargo)
  38. Quantity: 50
  39.  
  40. Type: Sensor Booster I (Fitted - Medium slot)
  41.  
  42. Type: Barton Reactor Capacitor Recharger I (Cargo)
  43. Quantity: 1
  44.  
  45. Type: Medium Automated Structural Restoration (Cargo)
  46. Quantity: 1
  47.  
  48. Type: Flameburst Light Missile I (Fitted - High slot)
  49. Quantity: 36
  50.  
  51. Type: Bloodclaw Light Missile I (Fitted - High slot)
  52. Quantity: 36
I dont even butter my bread. I consider that cooking
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 354
Reputation: Troy is an unknown quantity at this point 
Solved Threads: 5
Troy's Avatar
Troy Troy is offline Offline
Posting Whiz

Re: Parsing html form.

 
0
  #2
Jun 9th, 2005
So you want a process that will parse these emails to get specific bits of data, store this data in a database, then have an interface to display the data in a meaningful format?

If what I describe above is what you want, then you'd go about it by creating a PHP script that will use string manipulating and maybe regex (regular expressions) to parse the text of the email--breaking out all the individual data pieces you want. Then you build a sql statement to insert the data into your table. Finally, you build a script that selects the data and displays the summary data however you want.

The question now is, where do you need help? Are you a PHP programmer? Do you know how to connect to a mysql database in PHP? Are you familiar with using basic string manipulation functions in any programming language?

Also, do you have any control over the program that originally sends out these emails? Can you have that system send every email you care about to a special email address, you can have your server automatically check that email account to get the data. This way you would not have to copy & paste the email text and submit it to your script. If this is a controlled group of friends you are talking about, then you could have them all setup auto-forwarding rules to automatically forward their game emails to this special email account. It's an idea anyway.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 35
Reputation: croft is an unknown quantity at this point 
Solved Threads: 0
croft's Avatar
croft croft is offline Offline
Light Poster

Re: Parsing html form.

 
0
  #3
Jun 9th, 2005
So you want a process that will parse these emails to get specific bits of data, store this data in a database, then have an interface to display the data in a meaningful format?
This is exactly what i want =)

I am not a programmer. I know some stuff but very basic things. I have never written a php script myself. I do know how to connect to a database. And the database is no problem for me. The problem is in the coding of the parse script. I have no idea.

I have no control what so ever over the email program.
If anyone can help out it would be greatly apriciated =)
I dont even butter my bread. I consider that cooking
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 354
Reputation: Troy is an unknown quantity at this point 
Solved Threads: 5
Troy's Avatar
Troy Troy is offline Offline
Posting Whiz

Re: Parsing html form.

 
0
  #4
Jun 10th, 2005
Who loves you, croft? That's right, Troy does! Here is complete, working, tested code to parse your email body into the various data pieces. Just create the two files below, and place on a PHP enabled web server. Then surf to the eve_submit.htm web page.

NOTE: Text parsing is a tricky business. One small change in the text format may break your parsing script.

Create this HTML page: eve_submit.htm
  1. <html>
  2. <head>
  3. <title></title>
  4. </head>
  5. <body>
  6.  
  7. <form method="POST" action="eve_parse.php">
  8. <textarea name="email_body" cols="80" rows="20"></textarea>
  9. <br /><input type="submit" />
  10. </form>
  11.  
  12. </body>
  13. </html>

Create this PHP script: eve_parse.php
[php]
<?php

/*
Put the text that was passed in via HTTP into a variable.
*/
$email_body = $_REQUEST['email_body'];

/*
Create an array to hold the key -> value pairs.
*/
$data = array();

/* Create an array where each line of text is in an array element. */
$lines = split("\n", $email_body);

/*
Since the timestamp is the very first line of the text, go ahead and put this
into a variable.
*/
$data['timestamp'] = trim($lines[0]);

/*
Now iterate through the lines breaking each into it's key->value set.
Do this until we reach the "Destroyed items" section.
*/
for ($line_idx=1; $line_idx<count($lines); $line_idx++) {
$line = $lines[$line_idx];
$pair = split(":", trim($line));
$pair[0] = trim($pair[0]);
$pair[1] = trim($pair[1]);
if ($pair[0] == "Destroyed items") { break; }
if ((strlen($pair[0]) > 0) && (strlen($pair[1]) > 0)) {
$data[$pair[0]] = $pair[1];
}
}

/*
Now iterate through the destroyed items.
*/
$data['destroyed items'] = array();
for ($line_idx=$line_idx+1; $line_idx<count($lines); $line_idx++) {
$line = $lines[$line_idx];
$pair = split(":", trim($line));
$pair[0] = trim($pair[0]);
$pair[1] = trim($pair[1]);
if ((strlen($pair[0]) > 0) && (strlen($pair[1]) > 0)) {
if ($pair[0] == "Type") {
$item = array();
$item[] = $pair[1];

$line_idx++;
$line = $lines[$line_idx];
$pair = split(":", trim($line));
$pair[0] = trim($pair[0]);
$pair[1] = trim($pair[1]);
if ((strlen($pair[0]) > 0) && (strlen($pair[1]) > 0)) {
$item[] = $pair[1];
} else {
$item[] = 1;
}
$data['destroyed items'][] = $item;
}
}
}

/*
Dump the data array contents to the browser for testing purposes.
*/
echo "<pre>";
print_r($data);
echo "</pre>";

/*
You can directly access any item within the data array like so:
echo $data['Victim'];

To grab the values for the fourth destroyed item:
echo "Item #4 Name: ".$data['destroyed_items'][3][0];
echo "Item #4 Qty: ".$data['destroyed_items'][3][1];
(Remember, PHP arrays are zero-indexed, so counting starts at zero.)

Now you can insert whatever you want into your database.
*/
?>
[/php]

The program, when passed the email example you posted, outputs:
  1. Array
  2. (
  3. [timestamp] => 2005.06.04 03:35:00
  4. [Victim] => name of killed person here
  5. [Alliance] => Firmus Ixion
  6. [Corporation] => Corp name here
  7. [Destroyed Type] => Kestrel
  8. [Solar System] => H74-B0
  9. [System Security Level] => 0.0
  10. [Name] => name of killer here (laid the final blow)
  11. [Security Status] => 5.0
  12. [Ship Type] => Megathron
  13. [Weapon Type] => 425mm Railgun II
  14. [destroyed items] => Array
  15. (
  16. [0] => Array
  17. (
  18. [0] => Standard Missile Launcher I (Fitted - High slot)
  19. [1] => 1
  20. )
  21.  
  22. [1] => Array
  23. (
  24. [0] => Standard Missile Launcher I (Fitted - High slot)
  25. [1] => 1
  26. )
  27.  
  28. [2] => Array
  29. (
  30. [0] => Warp Disruptor I (Fitted - Medium slot)
  31. [1] => 1
  32. )
  33.  
  34. [3] => Array
  35. (
  36. [0] => Standard Missile Launcher I (Fitted - High slot)
  37. [1] => 1
  38. )
  39.  
  40. [4] => Array
  41. (
  42. [0] => Sabretooth Light Missile I (Cargo)
  43. [1] => 100
  44. )
  45.  
  46. [5] => Array
  47. (
  48. [0] => Bloodclaw Light Missile I (Cargo)
  49. [1] => 50
  50. )
  51.  
  52. [6] => Array
  53. (
  54. [0] => Bloodclaw Light Missile I (Cargo)
  55. [1] => 50
  56. )
  57.  
  58. [7] => Array
  59. (
  60. [0] => Sensor Booster I (Fitted - Medium slot)
  61. [1] => 1
  62. )
  63.  
  64. [8] => Array
  65. (
  66. [0] => Barton Reactor Capacitor Recharger I (Cargo)
  67. [1] => 1
  68. )
  69.  
  70. [9] => Array
  71. (
  72. [0] => Medium Automated Structural Restoration (Cargo)
  73. [1] => 1
  74. )
  75.  
  76. [10] => Array
  77. (
  78. [0] => Flameburst Light Missile I (Fitted - High slot)
  79. [1] => 36
  80. )
  81.  
  82. [11] => Array
  83. (
  84. [0] => Bloodclaw Light Missile I (Fitted - High slot)
  85. [1] => 36
  86. )
  87.  
  88. )
  89.  
  90. )
Troy Wolf is the author of SnippetEdit. "Website editing as easy as it gets." IX Web Hosting
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 35
Reputation: croft is an unknown quantity at this point 
Solved Threads: 0
croft's Avatar
croft croft is offline Offline
Light Poster

Re: Parsing html form.

 
0
  #5
Jun 10th, 2005
Thats great.
There is a couple of problems tho.

Array
(
[timestamp] => 2005.06.04 03:35:00
[Victim] => name of killed person here
[Alliance] => Firmus Ixion
[Corporation] => Corp name here
[Destroyed Type] => Kestrel
[Solar System] => H74-B0
[System Security Level] => 0.0
[Name] => name of killer here (laid the final blow)
[Security Status] => 5.0
[Ship Type] => Megathron
[Weapon Type] => 425mm Railgun II
[destroyed items] => Array
The victims alliance turns up as firmus ixions when the killer alliance is firmus ixion. The corp name for the killer and killed. The allliance for killer and killed.

Also. This wont add anything to a database unless you do it manually ? Or am i wrong here. Like me the players have no clue about this stuff and or how to add these things in to the database.
I dont even butter my bread. I consider that cooking
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 354
Reputation: Troy is an unknown quantity at this point 
Solved Threads: 5
Troy's Avatar
Troy Troy is offline Offline
Posting Whiz

Re: Parsing html form.

 
0
  #6
Jun 10th, 2005
Ah, yes. I missed that there is in fact a field that is duplicated between the "Victim" and "Involved Parties" sections. So you'd have to manipulate the code that breaks out those sections similar to how I broke out the destroyed items.

No, the code I provided does the retrieval and parsing. It does not insert the data into a database. I thought you only needed help with the parsing part because you said:
I do know how to connect to a database. And the database is no problem for me.
Here is the code example I give PHP newbies to learn how to work with a database.
[php]
<?php

$server = "localhost"; // Name or IP of database server.
$user = ""; // username
$pwd = ""; // password
$db = ""; // Name of database to connect to.

if (!$cnn = mysql_connect($server,$user,$pwd )) {
die("mysql_connect() failed");
}

if (!mysql_select_db($db,$cnn)) {
die("Could not select database named ".$db);
}

/* Build your SQL statement however you need. */
$sql = "select * from mytable";

/* Execute the query. */
if (!$res = @mysql_query($sql)) {
die(mysql_error());
return false;
}

/*
If the SQL statement above was an INSERT, UPDATE, or DELETE,
you'd be done now. If it was a SELECT, then this next part shows
you how to work with the recordset.
*/

/* Create an array of arrays out of the recordset. */
while ($row = mysql_fetch_array($res, MYSQL_ASSOC)) {
$data[] = $row;
}

/* Now iterate through the recordset creating a simple table. */
echo "<style>table.dump { font-family:Arial; font-size:8pt; }</style>";
echo "<table class=\"dump\" border=\"1\" cellpadding=\"1\" cellspacing=\"0\">\n";
echo "<tr>";
echo "<th>#</th>";
foreach($data[0] as $key=>$val) {
echo "<th><b>";
echo $key;
echo "</b></th>";
}
echo "</tr>\n";
$row_cnt = 0;
foreach($data as $row) {
$row_cnt++;
echo "<tr align='center'>";
echo "<td>".$row_cnt."</td>";
foreach($row as $val) {
echo "<td>";
echo $val;
echo "</td>";
}
echo"</tr>\n";
}
echo "</table>\n";

?>
[/php]

So now, croft, you have most of the code you need to do your project. You'll have to tweak and put the pieces all together, but almost everything you need is in these two code examples.

If you have additional specific questions regarding this project, post them here and I or somebody else will try to help. If you just want to pay someone to code the project for you, there are plenty of PHP gurus who'd be happy to code this according to your specs. Just let me know.
Troy Wolf is the author of SnippetEdit. "Website editing as easy as it gets." IX Web Hosting
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 35
Reputation: croft is an unknown quantity at this point 
Solved Threads: 0
croft's Avatar
croft croft is offline Offline
Light Poster

Re: Parsing html form.

 
0
  #7
Jun 10th, 2005
Its fine =)

I went out and bought me a book today. php and mysql bible. Have no idea if its any good but it was the only one i could find.
Now im gonna dive in it and check stuff out.
I dont even butter my bread. I consider that cooking
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1
Reputation: wds is an unknown quantity at this point 
Solved Threads: 0
wds wds is offline Offline
Newbie Poster

Re: Parsing html form.

 
0
  #8
Jun 13th, 2005
This is interesting and related to something I'm looking for. I want to be able to easily parse data from an HTML file. I have seen several classes people made in PHP and Java, but I was wondering if anyone did this already and could recommend a method? Thanks! Wes
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 354
Reputation: Troy is an unknown quantity at this point 
Solved Threads: 5
Troy's Avatar
Troy Troy is offline Offline
Posting Whiz

Re: Parsing html form.

 
0
  #9
Jun 13th, 2005
Originally Posted by wds
...I have seen several classes people made in PHP and Java, but I was wondering if anyone did this already and could recommend a method? Thanks! Wes
The classes you've already seen are the recommended methods from the people who already did this.

You haven't asked a specific question. You've asked about a general technique, in which case, my code post above is a big part of your answer.

Let us know how we can help you in your journey.
Troy Wolf is the author of SnippetEdit. "Website editing as easy as it gets." IX Web Hosting
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 35
Reputation: croft is an unknown quantity at this point 
Solved Threads: 0
croft's Avatar
croft croft is offline Offline
Light Poster

Re: Parsing html form.

 
0
  #10
Jun 13th, 2005
This is the code i ended up with at the end.
Thanks for the help.

[PHP]<?php

//killmail lines
$lines = split("\n", $email_body);

//get the timestamp
$timestamp = $lines[0];

/*Outline the plan....lets create three arrays:

Victim array:
* $victim[Victim]
* $victim[Alliance] (this will be null/blank if it doesn't exist)
* $victim[Corporation]
* $victim[Destroyed Type]
* $victim[Solar System]
* $victim[Security Level]

For each element (n) in the $involved array above you are dealing with an attacker
* $involved[n][Name]
* $involved[n][Security Status]
* $involved[n][Alliance] (this will be null/blank if it doesn't exist)
* $involved[n][Corporation]
* $involved[n][Ship Type]
* $involved[n][Weapon Type]
* $involved[n][Final Blow] (this is a boolean true/false)

The destoyed items array will have 3 fields for each element (n)
* $destroyed[n][Type]
* $destroyed[n][Location]
* $destroyed[n][Quantity]

*/

//lets find the location of the involved parties and destroyed items lines
for ($i = 0;$i < count($lines);$i++){
if(trim($lines[$i]) == "Involved parties:") {
$inv_loc = $i;
}else if(trim($lines[$i]) == "Destroyed items:") {
$dest_loc = $i;
}
}

//get victim info, start at line 2 go up to where we found the involved parties
for($i = 2;$i < $inv_loc;$i++){
if ( (trim($lines[$i]) != "")) {//working on a good line
unset($pair);
$pair = explode(": ", $lines[$i]);
$victim[$pair[0]] = $pair[1];
}
}

//lets assemble our killers by looking between the involved parties line and the destroyed items line
$inv_loc++; //add one so we start after the title line
for($i = $inv_loc;$i < $dest_loc;$i++){
$line = trim($lines[$i]);
if( ($line != "") && ($line != "**** Truncated - mail is too large ****") ){//working on a good line
unset($pair);
$pair = explode(": ", $line);

//clear array if on new killer
if($pair[0] == "Name") {
unset($killer);
if(substr_count($pair[1], "(laid the final blow)")) {
//found final killer get rid of final blow text
$pair[1] = substr($pair[1], 0, strlen($pair[1]) - 22);
$killer['Final Blow'] = 1;
}else {//not final killer
$killer['Final Blow'] = 0;
}
}

//add data onto killer array
$killer[$pair[0]] = $pair[1];

if($pair[0] == "Weapon Type") {
//finished, place the killer array onto the $involved stack
$involved[] = $killer;
}
}
}

//lets assemble the items that were destroyed
$dest_loc++;
for($i = $dest_loc; $i < count($lines); $i++){
$line = trim($lines[$i]);

if( ($line != "") && ($line != "**** Truncated - mail is too large ****") ){//working on a good line
unset($pair);
$pair = explode(": ", $line);

if($pair[0] != "Quantity") {//we only want the Type lines
unset($item);
//pull off the item name
$item_name = substr($pair[1], 0, strpos($pair[1], "("));
//pull off the location of the item; first pull from the last occurance of a ( then trim off the parenthesis
$item_loc = ltrim(rtrim(strrchr($pair[1], "("), ")"), "(");
$item['Type'] = $item_name;
$item['Location'] = $item_loc;
//check if this item has a quantity in the next line
$k = $i + 1;
if (substr_count($lines[$k], "Quantity: ")) {
$line = trim($lines[$k]);
unset($pair);
$pair = explode(": ", $line);
$item['Quantity'] = $pair[1];
}
//add this item to the stack
$destroyed[] = $item;
}

}

}

//now we're done lets echo out our data to see if we got it all right
echo "Victim: " . $victim['Victim'] . "<br>";
echo "Alliance: " . $victim['Alliance'] . "<br>";
echo "Corporation: " . $victim['Corporation'] . "<br>";
echo "Ship Lost: " . $victim['Destroyed Type'] . "<br>";
echo "Location: " . $victim['Solar System'] . "<br>";
echo "Security Level: " . $victim['Security Level'] . "<br>";
echo "<br>";

echo "There were " . count($involved) . " known involved parties.<br>";
echo "<br>";

foreach ($involved as $killer) {
if ($killer['Final Blow']) {
echo "<b>The final blow was laid by:</b><br>";
echo "<blockquote>";
echo "Name: " . $killer['Name'] . "<br>";
echo "Security Status: " . $killer['Security Status'] . "<br>";
echo "Alliance: " . $killer['Alliance'] . "<br>";
echo "Corporation: " . $killer['Corporation'] . "<br>";
echo "Ship Type: " . $killer['Ship Type'] . "<br>";
echo "Weapon Type: " . $killer['Weapon Type'] . "<br>";
echo "</blockquote>";
echo "<br>";
}
}

echo "<b>Other partied involved were:</b><br>";
echo "<blockquote>";

foreach ($involved as $killer) {
if(!$killer['Final Blow']) {
echo "Name: " . $killer['Name'] . "<br>";
echo "Security Status: " . $killer['Security Status'] . "<br>";
echo "Alliance: " . $killer['Alliance'] . "<br>";
echo "Corporation: " . $killer['Corporation'] . "<br>";
echo "Ship Type: " . $killer['Ship Type'] . "<br>";
echo "Weapon Type: " . $killer['Weapon Type'] . "<br>";
echo "<br>";
}
}
echo "</blockquote>";

echo "<br>";
echo "<b>Items destroyed in this ship loss:</b><br>";
echo "<blockquote>";

echo "<u>High Slot Items:</u><br>";
foreach ($destroyed as $item) {
if ($item['Location'] == "Fitted - High slot") {
//only high slot items have ammo
if($item['Quantity'] == ""){
echo $item['Type'] . "<br>";
}else {
echo $item['Quantity'] . " x " . $item['Type'] . "<br>";
}
}
}

echo "<br>";
echo "<u>Medium Slot Items:</u><br>";
foreach ($destroyed as $item) {
if ($item['Location'] == "Fitted - Medium slot") {
echo $item['Type'] . "<br>";
}
}

echo "<br>";
echo "<u>Low Slot Items:</u><br>";
foreach ($destroyed as $item) {
if ($item['Location'] == "Fitted - Low slot") {
echo $item['Type'] . "<br>";
}
}

echo "<br>";
echo "<u>Cargo Items:</u><br>";
foreach ($destroyed as $item) {
if ($item['Location'] == "Cargo") {
//looks like Eve counts weird here but meh, lets show quantity all the time setting nulls to 1
if ($item['Quantity'] == "") $item['Quantity'] = 1;
echo $item['Quantity'] . " x " . $item['Type'] . "<br>";
}
}
echo "</blockquote>";
?>[/PHP]
I dont even butter my bread. I consider that cooking
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC