PHP - 帮助追加textarea值

时间:2014-07-29 21:54:39

标签: php

我目前正在使用以下示例来处理textarea中的每个值,并将其拆分为name1 name2 name3,依此类推。我想在值的每一边附加一个引号,后跟逗号'name1', 'name2', 'name3'。最后,我想把所有这些都放在一个$string值中,我稍后可以参考。任何有关如何实现这一目标的建议都将受到赞赏。

<?php

//trim off excess whitespace off the whole
$text = trim($_POST['ServerName']);

//explode all separate lines into an array
$textAr = explode("\n", $text);

//trim all lines contained in the array.
$textAr = array_filter($textAr, 'trim');

//loop through the lines
foreach($textAr as $line){
echo "$line";

}

?>

2 个答案:

答案 0 :(得分:0)

  1. 添加引号:foreach ($textAr as $k=>$v) { $textAr[$k] = "'".$v."'"; }
  2. Concat:`$ text = implode(“,”,$ textAr);

答案 1 :(得分:0)

@ jason-000感谢您的帮助,我能够在做了更多研究后弄清楚您的意思。我删除了foreach循环,并能够按照以下方式输出我想要的方式 -

$text = trim($_POST['ServerName']);
$textAr = explode("\n", $text);
$values = array_values($textAr);
$string = implode("','", $values);
$string = "'".$string."'";
$output = "(".$string.")";

我最终得到了(&#39; Server1&#39;,&#39; Server2&#39;,#39; Server3&#39;),这很好。不知道为什么它在最后一个之前增加了空间。除了最后一个值。