tags:

views:

48

answers:

1

OK let's say I want to take this piece of text:

text ex 1.

  6,   6,   6,   6,   6,   6,   6,   6,  95,  95,  95,  51,
  6,  60,  60,  60,  60,  60,  60,   6,  95,  95,  95,  51,
  6,  60,  35,  35,  35,  35,  60,   6,   6,   6,  95,  95,
  6,  60,  35,  35,  35,  35,  60,  60,   6,   6,   6,   6,
  6,  60,  35,  35,  35,  35,  35,  60,   6,   6,   6,   6,
  6,  60,  35,  35,  35,  35,  35,  60,   6,  95,  95,  95,
  6,  60,  60,  21,  60,  60,  60,  60,   6,  95,  95,  51,
  6,   6,   6,   6,   6,   6,   6,   6,   6,  95,  95,  51

and want to turn it into

text ex 2.

{  6,   6,   6,   6,   6,   6,   6,   6,  95,  95,  95,  51, },
{  6,  60,  60,  60,  60,  60,  60,   6,  95,  95,  95,  51, },
{  6,  60,  35,  35,  35,  35,  60,   6,   6,   6,  95,  95, },
{  6,  60,  35,  35,  35,  35,  60,  60,   6,   6,   6,   6, },
{  6,  60,  35,  35,  35,  35,  35,  60,   6,   6,   6,   6, },
{  6,  60,  35,  35,  35,  35,  35,  60,   6,  95,  95,  95, },
{  6,  60,  60,  21,  60,  60,  60,  60,   6,  95,  95,  51, },
{  6,   6,   6,   6,   6,   6,   6,   6,   6,  95,  95,  51, },

using PHP. I would plop text ex 1. into a HTML form and using PHP... out would display for text ex 2...

How can this be achieved? Array...

+1  A: 

Probably something like this:

<?php

//Process the form post
if($_POST['text_to_format']){
    $sFormatText = fncProcessText($_POST['text_to_format']);
}else{
    $sFormatText = 'Put Text Here';
}

?>

<html>

    <form id="format" action="/test.php" method="post">
        <textarea name="text_to_format"><?php echo $sFormatText ?></textarea>
        <input type="submit" value="Format">
    </form>

</html>

<?php

//Text Formatting Fuction
function fncProcessText( $sText = null ){

    //Make sure something is in $sText
    if($sText){
        //Create an array by splitting on line breaks
        $aText = explode("\r\n", $sText);

        //Glue the array together with the desired formatting
        return "{" . implode("},{\n", $aText) . "},";
    }else{
        return 'Put Text Here';
    }
}

?>
paperreduction
Looks promising! However the function fncProccessText makes the HTML form disappear. When I remove the function... the form appears...
Dan
I noticed that it was missing a semi-colon after return and changed action to "filenamehere.php" and method to "post" and now I am getting back "{Array}," inside the Textarea
Dan
sorry, i didn't test that code before posting it. i'll take a look.
paperreduction
Fixed it! Nice. But anyway so it's formatted nicely... right now it's like this:{ 6, 6, 6, 6, 6, 6, 6, 6, 95, 95, 95, 51,},{ 6, 60, 60, 60, 60, 60, 60, 6, 95, 95, 95, 51,
Dan
I mean in here `51, },{ 6, 60,` there is a line break between the `,` and `{` so the `{` is on a line all by itself...
Dan
okay, so are you saying there is an extra line break?
paperreduction
try exploding on "\r\n"... I just updated the code above to do just that.
paperreduction
Got it :) Thanks!!
Dan