PHP - 从Textarea中删除换行符

PHP  - 从Textarea中删除换行符

问题描述:

I was wondering what I am missing in my code. I have a large form that is pushing all values to a .csv file. There are instances of textareas and every time I put some text content in and add a line break (hit the enter-key) within the .csv document, any line of text after the first breaks the flow of the values within the .csv, and starts a new line.

I've tried checking taking the value via php and removing any spaces or breaks, but it doesn't seem to be working.

Can anyone tell me what I am missing?

HTML:

<textarea id="fianlCommentsText" name="fianlCommentsText"></textarea>

PHP:

$finalCommentsText = $_POST["finalCommentsText"];
function finalCommentsLineCheck()
    {
        global $finalCommentsText;
        preg_replace( "/|
/", "", $finalCommentsText );
    }
    finalCommentsLineCheck();

我想知道我的代码中缺少什么。 我有一个大型表单,将所有值推送到 .csv文件 code>。 有 textareas code>的实例,每次我放入一些文本内容并在 .csv code>文档中添加换行符(按Enter键),任何文本行 在第一次打破 .csv code>中的值之后,开始一个新行。 p>

我已经尝试通过php检查获取值并删除 任何空格或休息,但它似乎没有工作。 p>

谁能告诉我我缺少什么? p>

HTML : strong> p>

 &lt; textarea id =“fianlCommentsText”name =“fianlCommentsText”&gt;&lt; / textarea&gt; 
  code>  pre> \  n 
 

PHP: strong> p>

  $ finalCommentsText = $ _POST [“finalCommentsText”]; 
 function finalCommentsLineCheck()
 {
  global $ finalCommentsText; 
 preg_replace(“/ 
 | 
 /”,“”,$ finalCommentsText); 
} 
 finalCommentsLineCheck(); 
  code>  pre> 
  div>

The "choice" of or requires parens:

preg_replace( "/(|
)/", "", $finalCommentsText );

Try that?

In order to allow multiline fields in csv, you need to quote them with "

For example:

12345,"multiline
text",another_field

In case you have quotes inside your text, just escape them with another quote:

12345,"27"" monitor 
TFT",another_field

This way you can keep the newlines inside the text.

In my case I solved in this way:

Output to a textarea removing the ' ' characters:

<!-- this textarea will show blank carriage return and not 
 characters -->
<textarea  class="settinginput" id="message" name="message">
<?php echo str_replace('
',"
",$message); ?>
</textarea>

After submit here is how to replace the carriage returns with the ' ' not expanded characters:

str_replace("
",'
',$_POST['message'])

Note that the replace function uses double quotes and single quotes in order to save the string with no expansion for escape sequences.