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
https://forums.phpfreaks.com/topic/76460-solved-explode-multiple-lines/
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>";

?>

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

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.