开发者

PHP - a good way to split up into lines and add prefix and suffix

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


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

?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜