Alex020691 Posted November 8, 2007 Share Posted November 8, 2007 Hey everyone, I'm VERY new to php, only started learning a couple of days ago. Basically, I'm trying to write a script to convert Co-ordinates in the form 'a! b! c!' into '20+a! 20+b! 20+c!'. I've managed to figure out how to do one line, but am completely stumped upon trying to work out the next line. I've done a basic HTML textarea form being processed to the .php file, with these variables: $origdata = $_POST['origdata']; $origdata = preg_replace('/(\\s){2,}/', '$1', $origdata); Followed by: $pieces = explode(" ", $origdata); echo 20 + $pieces[0].'!'.' '; echo 20 + $pieces[1].'!'.' '; echo 20 + $pieces[2].'!'.' '; Which basically turns, for example, '-719.473! 257.075! 10.105!' into '-699.473! 277.075! 30.105!'. Any help would be appreciated, thanks. Here is a screenshot of my progress, so you should be able to get the general idea, because I'm really bad at explaining things. http://i44.photobucket.com/albums/f3/venomxsigs/inputoutput.png Quote Link to comment Share on other sites More sharing options...
alecks Posted November 8, 2007 Share Posted November 8, 2007 I just did this really quickly, hope it helps <?php echo "<form action=\"test.php\" method=\"post\">" ."<textarea name=\"origdata\">".$_POST['origdata']."</textarea>" ."<br /><input type=\"submit\" value=\"convert\" />" ."</form>"; echo "<textarea name=\"origdata\">"; if (isset($_POST['origdata'])){ $return = preg_replace('/(\\s){2,}/', '$1', $_POST['origdata']); $return = explode(" ", $return); } foreach ($return as $piece){ echo $piece + 20; echo " "; } echo "</textarea>"; ?> Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted November 8, 2007 Share Posted November 8, 2007 Try: <?php echo '<form action="test.php" method="post"> <textarea name="origdata" rows="10" cols="60">'.@$_POST['origdata'].'</textarea><br /> <input type="submit" value="convert" /> </form>'; if (isset($_POST['origdata'])) { echo '<textarea name="origdata" rows="10" cols="60">'; $data = str_replace(array("\r\n", "\r", "\n"), "\n", $_POST['origdata']); $data = preg_replace('/(\\s){2,}/', '$1', $data); $lines = explode("\n", $data); foreach($lines as $line) { $coords = explode('! ', $line); foreach($coords as $k => $coord) { echo str_pad(($coord + 20), 3, '0', STR_PAD_LEFT) . '! '; } echo "\n"; } } echo '</textarea>'; ?> Quote Link to comment Share on other sites More sharing options...
Alex020691 Posted November 9, 2007 Author Share Posted November 9, 2007 Thanks SO much to both of you, I combined what you said with the little knowledge I have and now the page works perfectly, and I can continue with my learning . Thanks again, topic solved. 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.