drxdai15012937753
2016-10-26 13:40
浏览 54
已采纳

PHP - 防止动态创建文本区域中的换行符

I have a form with sets of inputs that can be dynamically created -- see here.

In order to push the values gathered from these inputs to a .csv sheet and keep them together in a single cell, I am using the following PHP function. It gathers the content from the 3 inputs, saves it to the .csv in a single cell, and for every new iteration of inputs, it just separates the entries with a "//" but keeps it in the same cell (like this: President, 2015, I was the president// Vice President, 2014, I was the vice president).

PHP

$provincialInvolvement = $_POST["provincialInvolvement"];
$provincialInvolvementValues = "";
$e = 0;

foreach($provincialInvolvement as $piValue)
    {
        $provincialInvolvementValues .= $piValue;
        $e++;
        if($e % 3 == 0)
            {
                $provincialInvolvementValues .= "//";
            }

    }

My problem is because the last input is a textarea, if a line break is put in (someone hits the enter key) it breaks the form of the .csv and puts it on a new line.

I am trying to write a function onto the portion of my PHP that, as it logs the value into $provincialInvolvementValues it removes any linebreaks but I;m having no luck.

Here is the function problem is it doesn't log anything.

$provincialInvolvement = $_POST["provincialInvolvement"];
$provincialInvolvementValues = "";
$e = 0;

foreach($provincialInvolvement as $piValue)
    {
        $provincialInvolvementValues .= $piValue;
        $e++;
        $provincialInvolvementValues = preg_replace( "/(|
)/ ", "", $provincialInvolvementValues ); 
        return $provincialInvolvementValues;
        if($e % 3 == 0)
            {
                $provincialInvolvementValues .= "//";
            }

    }

图片转代码服务由CSDN问答提供 功能建议

我有一个包含可以动态创建的输入集的表单 - 请参见此处

为了将从这些输入收集的值推送到。 csv 表并将它们放在一个单元格中,我使用以下PHP函数。 它从3个输入中收集内容,将其保存到单个单元格中的 .csv ,并且对于每个新的输入迭代,它只是用“//”分隔条目,但保留它 同一个小组(像这样:总统,2015年,我是总裁//副总裁,2014年,我是副总裁)。

PHP

  $ provincialInvolvement = $ _POST [“provincialInvolvement”]; 
 $ provincialInvolvementValues =“”; 
 $ e = 0; 
 
foreach($ provincialInvolvement as $ piValue)\  n {
 $ provincialInvolvementValues。= $ piValue; 
 $ e ++; 
 if($ e%3 == 0)
 {
 $ provincialInvolvementValues。=“//”; 
} 
 
}  
   
 
 

我的问题是因为最后一个输入是textarea,如果放入换行符(有人点击回车键)它会打破 .csv 并将它放在一个新行上。

我正在尝试将函数写入我的PHP部分,因为它将值记录到 $ provincialInvolvementValues 它删除了任何换行符,但我没有运气。

这是函数问题,它没有记录任何内容。 $ provinceInvolvement = $ _POST [“provincialInvolvement”]; $ provincialInvolvementValues =“”; $ e = 0; foreach($ provincialInvolvement as $ piValue) { $ provincialInvolvementValues。= $ piValue; $ e ++; $ provincialInvolvementValues = preg_replace(“/( | )/”,“”,$ provincialInvolvementValues); 返回$ provincialInvolvementValues; if($ e%3 == 0) { $ provincialInvolvementValues。=“//”; } }

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dsvjmc0907 2016-10-26 14:12
    已采纳

    Maybe try this: preg_replace( "? \s*", "", $provincialInvolvementValues) - newlines are usually or so this might work better.

    Also, the return $provincialInvolvementValues; is probably wrong in the loop...

    打赏 评论

相关推荐 更多相似问题