PHP -从文本区删除换行符

PHP - Remove Line Breaks From Textarea

本文关键字:删除 换行符 文本区 PHP      更新时间:2023-09-26

我想知道我在我的代码中缺少什么。我有一个大的形式,将所有的值都压入.csv file。有textareas的实例,每次我在.csv文档中放入一些文本内容并添加换行符(按回车键)时,第一行之后的任何一行文本都会打破.csv中值的流,并开始一个新行。

我试过检查通过php取值并删除任何空格或break,但它似乎不工作。

谁能告诉我我错过了什么?

HTML:

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

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

'r或'n的"choice"需要父元素:

preg_replace( "/('r|'n)/", "", $finalCommentsText );

试试?

在我的例子中我是这样解决的:

输出到文本区域,去掉''n'字符:

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

提交后,这里是如何用''n'不展开的字符替换回车:

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

请注意,replace函数使用双引号和单引号来保存字符串,而不扩展转义序列。

为了允许csv中的多行字段,您需要用"引用它们

例如:

12345,"multiline
text",another_field

如果你的文本中有引号,只需用另一个引号转义:

12345,"27"" monitor 
TFT",another_field

这样就可以将换行符保留在文本中。