...

View Full Version : Not Exploding Correctly?



Big-Pat
01-11-2008, 06:54 AM
It isn't exploding correctly, I think it is the first one:
$spell = explode("\n"."\n", $text);

link (http://dnd.theunlink.com/index.php)
Complete(but not finished)

<?php

$text = $_POST['text'];

$spell = explode("\n"."\n", $text);

for($i=0; $spell[$i]; $i++)
{
$line[$i] = explode("\n", $spell[$i]);
}

for($i=0; $line[$i]; $i++)
{
for($j=0; $line[$i][$j]; $j++)
{
$atributes[$i][$j] = explode(":", $line[$i][$j]);
}
}

for($i=0; $atributes[$i]; $i++)
{
echo "Spell: ".$atributes[$i][0][0]."<br>";
echo "School: ".$atributes[$i][1][0]."<br>";
echo "<br>";
}
?>
A piece of the submitted text:

Acid Arrow
Conjuration (Creation) [Acid]
Level: Sor/Wiz 2
Components: V, S, M, F
Casting Time: 1 standard action
Range: Long (400 ft. + 40 ft./level)
Effect: One arrow of acid
Duration: 1 round + 1 round per three levels
Saving Throw: None
Spell Resistance: No
A magical arrow of acid springs from your hand and speeds to its target. You must succeed on a ranged touch attack to hit your target. The arrow deals 2d4 points of acid damage with no splash damage. For every three caster levels (to a maximum of 18th), the acid, unless somehow neutralized, lasts for another round, dealing another 2d4 points of damage in that round.
Material Component: Powdered rhubarb leaf and an adder’s stomach.
Focus: A dart.

Acid Fog
Conjuration (Creation) [Acid]
Level: Sor/Wiz 6, Water 7
Components: V, S, M/DF
Casting Time: 1 standard action
Range: Medium (100 ft. + 10 ft./level)
Effect: Fog spreads in 20-ft. radius, 20 ft. high
Duration: 1 round/level
Saving Throw: None
Spell Resistance: No
Acid fog creates a billowing mass of misty vapors similar to that produced by a solid fog spell. In addition to slowing creatures down and obscuring sight, this spell’s vapors are highly acidic. Each round on your turn, starting when you cast the spell, the fog deals 2d6 points of acid damage to each creature and object within it.
Arcane Material Component: A pinch of dried, powdered peas combined with powdered animal hoof.

Acid Splash
Conjuration (Creation) [Acid]
Level: Sor/Wiz 0
Components: V, S
Casting Time: 1 standard action
Range: Close (25 ft. + 5 ft./2 levels)
Effect: One missile of acid
Duration: Instantaneous
Saving Throw: None
Spell Resistance: No
You fire a small orb of acid at the target. You must succeed on a ranged touch attack to hit your target. The orb deals 1d3 points of acid damage.
var_dump() of $atributes
array(1) {
[0]=>
array(65) {
[0]=>
array(1) {
[0]=>
string(11) "Acid Arrow
"
}
[1]=>
array(1) {
[0]=>
string(30) "Conjuration (Creation) [Acid]
"
}
[2]=>
array(2) {
[0]=>
string(5) "Level"
[1]=>
string(11) " Sor/Wiz 2
"
}
[3]=>
array(2) {
[0]=>
string(10) "Components"
[1]=>
string(12) " V, S, M, F
"
}
[4]=>
array(2) {
[0]=>
string(12) "Casting Time"
[1]=>
string(19) " 1 standard action
"
}
[5]=>
array(2) {
[0]=>
string(5) "Range"
[1]=>
string(31) " Long (400 ft. + 40 ft./level)
"
}
[6]=>
array(2) {
[0]=>
string(6) "Effect"
[1]=>
string(19) " One arrow of acid
"
}
[7]=>
array(2) {
[0]=>
string(8) "Duration"
[1]=>
string(36) " 1 round + 1 round per three levels
"
}
[8]=>
array(2) {
[0]=>
string(12) "Saving Throw"
[1]=>
string(6) " None
"
}
[9]=>
array(2) {
[0]=>
string(16) "Spell Resistance"
[1]=>
string(4) " No
"
}
[10]=>
array(1) {
[0]=>
string(369) "A magical arrow of acid springs from your hand and speeds to its target. You must succeed on a ranged touch attack to hit your target. The arrow deals 2d4 points of acid damage with no splash damage. For every three caster levels (to a maximum of 18th), the acid, unless somehow neutralized, lasts for another round, dealing another 2d4 points of damage in that round.
"
}
[11]=>
array(2) {
[0]=>
string(18) "Material Component"
[1]=>
string(47) " Powdered rhubarb leaf and an adder’s stomach.
"
}
[12]=>
array(2) {
[0]=>
string(5) "Focus"
[1]=>
string(9) " A dart.
"
}
[13]=>
array(1) {
[0]=>
string(1) "
"
}
[14]=>
array(1) {
[0]=>
string(9) "Acid Fog
"
}
[15]=>
array(1) {
[0]=>
string(30) "Conjuration (Creation) [Acid]
"
}
[16]=>
array(2) {
[0]=>
string(5) "Level"
[1]=>
string(20) " Sor/Wiz 6, Water 7
"
}
[17]=>
array(2) {
[0]=>
string(10) "Components"
[1]=>
string(12) " V, S, M/DF
"
}
[18]=>
array(2) {
[0]=>
string(12) "Casting Time"
[1]=>
string(19) " 1 standard action
"
}
[19]=>
array(2) {
[0]=>
string(5) "Range"
[1]=>
string(33) " Medium (100 ft. + 10 ft./level)
"
}
[20]=>
array(2) {
[0]=>
string(6) "Effect"
[1]=>
string(43) " Fog spreads in 20-ft. radius, 20 ft. high
"
}
[21]=>
array(2) {
[0]=>
string(8) "Duration"
[1]=>
string(15) " 1 round/level
"
}
[22]=>
array(2) {
[0]=>
string(12) "Saving Throw"
[1]=>
string(6) " None
"
}
[23]=>
array(2) {
[0]=>
string(16) "Spell Resistance"
[1]=>
string(4) " No
"
}
[24]=>
array(1) {
[0]=>
string(333) "Acid fog creates a billowing mass of misty vapors similar to that produced by a solid fog spell. In addition to slowing creatures down and obscuring sight, this spell’s vapors are highly acidic. Each round on your turn, starting when you cast the spell, the fog deals 2d6 points of acid damage to each creature and object within it.
"
}
[25]=>
array(2) {
[0]=>
string(25) "Arcane Material Component"
[1]=>
string(69) " A pinch of dried, powdered peas combined with powdered animal hoof.
"
}
[26]=>
array(1) {
[0]=>
string(1) "
"
}
[27]=>
array(1) {
[0]=>
string(12) "Acid Splash
"
}
[28]=>
array(1) {
[0]=>
string(30) "Conjuration (Creation) [Acid]
"
}
[29]=>
array(2) {
[0]=>
string(5) "Level"
[1]=>
string(11) " Sor/Wiz 0
"
}
[30]=>
array(2) {
[0]=>
string(10) "Components"
[1]=>
string(6) " V, S
"
}
[31]=>
array(2) {
[0]=>
string(12) "Casting Time"
[1]=>
string(19) " 1 standard action
"
}
[32]=>
array(2) {
[0]=>
string(5) "Range"
[1]=>
string(33) " Close (25 ft. + 5 ft./2 levels)
"
}
[33]=>
array(2) {
[0]=>
string(6) "Effect"
[1]=>
string(21) " One missile of acid
"
}
[34]=>
array(2) {
[0]=>
string(8) "Duration"
[1]=>
string(15) " Instantaneous
"
}
[35]=>
array(2) {
[0]=>
string(12) "Saving Throw"
[1]=>
string(6) " None
"
}
[36]=>
array(2) {
[0]=>
string(16) "Spell Resistance"
[1]=>
string(4) " No
"
}
[37]=>
array(1) {
[0]=>
string(147) "You fire a small orb of acid at the target. You must succeed on a ranged touch attack to hit your target. The orb deals 1d3 points of acid damage.
"
}
[38]=>
array(1) {
[0]=>
string(1) "
"
}
[39]=>
array(1) {
[0]=>
string(4) "Aid
"
}
[40]=>
array(1) {
[0]=>
string(42) "Enchantment (Compulsion) [Mind-Affecting]
"
}
[41]=>
array(2) {
[0]=>
string(5) "Level"
[1]=>
string(23) " Clr 2, Good 2, Luck 2
"
}
[42]=>
array(2) {
[0]=>
string(10) "Components"
[1]=>
string(10) " V, S, DF
"
}
[43]=>
array(2) {
[0]=>
string(12) "Casting Time"
[1]=>
string(19) " 1 standard action
"
}
[44]=>
array(2) {
[0]=>
string(5) "Range"
[1]=>
string(7) " Touch
"
}
[45]=>
array(2) {
[0]=>
string(6) "Target"
[1]=>
string(25) " Living creature touched
"
}
[46]=>
array(2) {
[0]=>
string(8) "Duration"
[1]=>
string(14) " 1 min./level
"
}
[47]=>
array(2) {
[0]=>
string(12) "Saving Throw"
[1]=>
string(6) " None
"
}
[48]=>
array(2) {
[0]=>
string(16) "Spell Resistance"
[1]=>
string(16) " Yes (harmless)
"
}
[49]=>
array(1) {
[0]=>
string(210) "Aid grants the target a +1 morale bonus on attack rolls and saves against fear effects, plus temporary hit points equal to 1d8 + caster level (to a maximum of 1d8+10 temporary hit points at caster level 10th).
"
}
[50]=>
array(1) {
[0]=>
string(1) "
"
}
[51]=>
array(1) {
[0]=>
string(9) "Air Walk
"
}
[52]=>
array(1) {
[0]=>
string(20) "Transmutation [Air]
"
}
[53]=>
array(2) {
[0]=>
string(5) "Level"
[1]=>
string(21) " Air 4, Clr 4, Drd 4
"
}
[54]=>
array(2) {
[0]=>
string(10) "Components"
[1]=>
string(10) " V, S, DF
"
}
[55]=>
array(2) {
[0]=>
string(12) "Casting Time"
[1]=>
string(19) " 1 standard action
"
}
[56]=>
array(2) {
[0]=>
string(5) "Range"
[1]=>
string(7) " Touch
"
}
[57]=>
array(2) {
[0]=>
string(6) "Target"
[1]=>
string(42) " Creature (Gargantuan or smaller) touched
"
}
[58]=>
array(2) {
[0]=>
string(8) "Duration"
[1]=>
string(15) " 10 min./level
"
}
[59]=>
array(2) {
[0]=>
string(12) "Saving Throw"
[1]=>
string(6) " None
"
}
[60]=>
array(2) {
[0]=>
string(16) "Spell Resistance"
[1]=>
string(16) " Yes (harmless)
"
}
[61]=>
array(1) {
[0]=>
string(227) "The subject can tread on air as if walking on solid ground. Moving upward is similar to walking up a hill. The maximum upward or downward angle possible is 45 degrees, at a rate equal to one-half the air walker’s normal speed.
"
}
[62]=>
array(1) {
[0]=>
string(361) "A strong wind (21+ mph) can push the subject along or hold it back. At the end of its turn each round, the wind blows the air walker 5 feet for each 5 miles per hour of wind speed. The creature may be subject to additional penalties in exceptionally strong or turbulent winds, such as loss of control over movement or physical damage from being buffeted about.
"
}
[63]=>
array(1) {
[0]=>
string(479) "Should the spell duration expire while the subject is still aloft, the magic fails slowly. The subject floats downward 60 feet per round for 1d6 rounds. If it reaches the ground in that amount of time, it lands safely. If not, it falls the rest of the distance, taking 1d6 points of damage per 10 feet of fall. Since dispelling a spell effectively ends it, the subject also descends in this way if the air walk spell is dispelled, but not if it is negated by an antimagic field.
"
}
[64]=>
array(1) {
[0]=>
string(243) "You can cast air walk on a specially trained mount so it can be ridden through the air. You can train a mount to move with the aid of air walk (counts as a trick; see Handle Animal skill) with one week of work and a DC 25 Handle Animal check.
"

arnyinc
01-11-2008, 02:49 PM
I don't see any issue with the explode call. If you call print_r($spells) right after that, it will print out the array that was created.

I think you need to re-work some of the for loops at the bottom. Particularly, I am not familiar with this syntax: for($i=0; $line[$i]; $i++). I'm not sure what the $line[$i] condition is supposed to be doing. Typically you would compare $i to the size of the array: for($i=0; $i<=count($line); $i++).

Here is some code I wrote up real quick. I changed the data by adding a "spell:" and "school:" identifier but this gives you a nice associative indexed array separated by each spell:



<?php
$text='
Spell: Acid Arrow
School: Conjuration (Creation) [Acid]
Level: Sor/Wiz 2
Components: V, S, M, F
Casting Time: 1 standard action
Range: Long (400 ft. + 40 ft./level)
Effect: One arrow of acid
Duration: 1 round + 1 round per three levels
Saving Throw: None
Spell Resistance: No
A magical arrow of acid springs from your hand and speeds to its target. You must succeed on a ranged touch attack to hit your target. The arrow deals 2d4 points of acid damage with no splash damage. For every three caster levels (to a maximum of 18th), the acid, unless somehow neutralized, lasts for another round, dealing another 2d4 points of damage in that round.
Material Component: Powdered rhubarb leaf and an adderís stomach.
Focus: A dart.

Spell: Acid Fog
School: Conjuration (Creation) [Acid]
Level: Sor/Wiz 6, Water 7
Components: V, S, M/DF
Casting Time: 1 standard action
Range: Medium (100 ft. + 10 ft./level)
Effect: Fog spreads in 20-ft. radius, 20 ft. high
Duration: 1 round/level
Saving Throw: None
Spell Resistance: No
Acid fog creates a billowing mass of misty vapors similar to that produced by a solid fog spell. In addition to slowing creatures down and obscuring sight, this spellís vapors are highly acidic. Each round on your turn, starting when you cast the spell, the fog deals 2d6 points of acid damage to each creature and object within it.
Arcane Material Component: A pinch of dried, powdered peas combined with powdered animal hoof.

Spell: Acid Splash
School: Conjuration (Creation) [Acid]
Level: Sor/Wiz 0
Components: V, S
Casting Time: 1 standard action
Range: Close (25 ft. + 5 ft./2 levels)
Effect: One missile of acid
Duration: Instantaneous
Saving Throw: None
Spell Resistance: No
You fire a small orb of acid at the target. You must succeed on a ranged touch attack to hit your target. The orb deals 1d3 points of acid damage.
';

$spells = explode("\n"."\n", $text);
$i=0;

foreach($spells as $spell)
{
$spellRows = explode("\n", $spell);
foreach($spellRows as $spellRow)
{
list($attributeName, $attributeValue)=explode(":", $spellRow);
$attributes[$i][$attributeName]=$attributeValue;
}
$i++;
}


print "<pre>";
print_r($attributes);
print "</pre>";
?>

StupidRalph
01-11-2008, 04:02 PM
You just said it isn't "exploding correctly". What is your expected outcome? And what is the actual outcome? And why do you concatenate "\n" within the first parameter instead of just having them in one string?

Did you mean to explode on \r\n instead of \n\n?

Big-Pat
01-11-2008, 05:44 PM
I don't see any issue with the explode call. If you call print_r($spells) right after that, it will print out the array that was created.

I think you need to re-work some of the for loops at the bottom. Particularly, I am not familiar with this syntax: for($i=0; $line[$i]; $i++). I'm not sure what the $line[$i] condition is supposed to be doing. Typically you would compare $i to the size of the array: for($i=0; $i<=count($line); $i++).

Here is some code I wrote up real quick. I changed the data by adding a "spell:" and "school:" identifier but this gives you a nice associative indexed array separated by each spell:

"for($i=0; $line[$i]; $i++)" basically is a while loop (while something is in $line[$i]), but I put it in a for just to make it a little cleaner.
I can't change the text being inputted, there are way to many to change. The point is to eventually put them into a database.


You just said it isn't "exploding correctly". What is your expected outcome? And what is the actual outcome? And why do you concatenate "\n" within the first parameter instead of just having them in one string?

Did you mean to explode on \r\n instead of \n\n?

Actually it looks like \n\r works how I need it to. Thanks, I didn't know there was a difference between \n and \r.

StupidRalph
01-11-2008, 10:31 PM
\r is a carriage return
\n is a new line

Windows uses a combination of a carriage return and new line when you press the Enter key. The carriage return (\r) moves the cursor back to the left side of the window. The new line (\n) starts a new line beneath.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum