Jump to content

[SOLVED] Explode multiple lines


Alex020691

Recommended Posts

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

Link to comment
Share on other sites

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

?>

Link to comment
Share on other sites

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

?>

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.