简体   繁体   English

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

[英]PHP - Preventing Line breaks in Dynamically Created Text area

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

PHP 的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. 我的问题是,因为最后一个输入是文本区域,如果输入了换行符(有人按Enter键),它将破坏.csv的形式并将其放在新行上。

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. 我正在尝试在PHP的一部分上编写一个函数,因为它将值记录到$provincialInvolvementValues因此可以删除任何换行符,但是我没有运气。

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( "/(\r|\n)/ ", "", $provincialInvolvementValues ); 
        return $provincialInvolvementValues;
        if($e % 3 == 0)
            {
                $provincialInvolvementValues .= "//";
            }

    }

Maybe try this: preg_replace( "\\r?\\n\\s*", "", $provincialInvolvementValues) - newlines are usually \\n or \\r\\n so this might work better. 也许试试看: preg_replace( "\\r?\\n\\s*", "", $provincialInvolvementValues) -换行符通常是\\n\\r\\n所以这样做可能更好。

Also, the return $provincialInvolvementValues; 同样, return $provincialInvolvementValues; is probably wrong in the loop... 循环中可能是错误的...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM