2010-07-24 21 views
0

OK,让我们说,我想借此一段文字:PHP - 一个很好的方式来分割成线,并添加前缀和后缀

文本前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 

,并希望将其到

文本的前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, }, 

使用PHP。我会将文本ex 1.放入HTML表格并使用PHP ... out将显示文本ex 2 ...

这怎么能实现?阵...

回答

1

大概是这样的:

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

看起来有前途的!但函数fncProccessText使HTML表单消失。当我删除函数...窗体出现... – nn2 2010-07-24 19:21:15

+0

我注意到它返回后缺少一个分号,并将操作更改为“filenamehere.php”和方法“发布”,现在我回来了“{ Array}“,”在Textarea – nn2 2010-07-24 19:26:41

+0

内部,对不起,我没有在发布之前测试该代码。我会看一看。 – paperreduction 2010-07-24 19:29:47