2011-07-09 65 views
0

有一个用户textarea输入。我需要在保存之前删除所有新行(所以它应该只有一行)。无法删除新行

我用

$text = str_replace("\r\n", "", $text); 
$text = str_replace("\n", "", $text); 

这一点。

但是,一个用户输入了文字,并且有新的一行,并且无法用该代码删除。
我也试过\r\n\r没有结果。

我不能在这里复制它,因为只要我复制它,新行将被替换为标准\n

如何删除此新行?或者我怎么能看到它的特征是什么?
它现在保存在MySQL中。

+0

可能重复(http://stackoverflow.com/questions/2274506/reliably-remove-newlines-from-string) – Gordon

+0

不,它不是重复的。我在那里找不到答案。 – Qiao

+0

好吧,如果这不是重复的,那么如何http://stackoverflow.com/questions/5449580/replacing-rn-with-php或http://stackoverflow.com/questions/3654844/php-str-replace-不工作正确或http://stackoverflow.com/questions/3779464/php-simple-way-to-replace-or-remove-empty-lines-with-str-replace或http://stackoverflow.com/问题/ 5057405 /换行符问题。如果你仍然坚持认为它不是重复的,你应该更新你的问题,指出它为什么不是重复的,因为我很难看出你的问题和那些问题有什么不同。 – Gordon

回答

2

你试过:

后澄清

// Order of replacement 
$str  = "Line 1\nLine 2\rLine 3\r\nLine 4\n"; 
$order = array("\r\n", "\n", "\r"); 
$replace = ''; 

// Processes \r\n's first so they aren't converted twice. 
$newstr = str_replace($order, $replace, $str); 

这是从PHP手册直接:http://php.net/manual/en/function.str-replace.php

的[可靠地除去从换行字符串]
+0

新行在字符串中。 – Qiao

+0

感谢澄清...更新了答案 – sdolgy

+0

手册中的代码也不起作用。是和我一样,只是使用数组。新线路仍在这里。所有其他新线都可以正常工作。 – Qiao