2016-08-12 39 views
0

我有這個PHP代碼PHP去除黑新線路

$Text=htmlspecialchars($_POST['new_post'],ENT_QUOTES); 
    $Text=trim($Text); 

Text是可變的,由user.If text定義的

There is a pleasure in the pathless woods, 
There is a rapture on the lonely shore, 
There is society, where none intrudes. 

我希望它留樣that.But如果Text

By the deep Sea, and music in its roar: 


I love not Man the less, but Nature more, 



From these our interviews, in which I steal 

我希望它是

By the deep Sea, and music in its roar: 
I love not Man the less, but Nature more, 
From these our interviews, in which I steal 

我該如何用PHP實現?

+4

的可能的複製[?我如何刪除文本在PHP中的空行] (http://stackoverflow.com/questions/709669/how-do-i-remove-blank-lines-from-text-in-php) – Script47

+0

我做到了,但它沒有奏效。@ Script47 –

+0

向我們展示你的html輸出 – Takarii

回答

0

一個簡單的解決方案是使用preg_replace()。這將找到任何多行,並用一個單一的新行替換它們:

$Text = preg_replace("/[\r\n]+/", "\n", $Text); 

看到一個演示here

$Text = "There is a pleasure in the pathless woods, 


There is a rapture on the lonely shore, 


There is society, where none intrudes."; 


$Text=trim($Text); 
$Text=preg_replace("/[\r\n]+/", "\n", $Text); 

echo $Text; 
+0

謝謝!有效 –

0

您可以使用str_replace函數這個

str_replace(array("\n", "\r"), '', $text); 

而且你在輸入,而不是輸出轉義的原因嗎?

+0

我插入到db那就是爲什麼。並沒有區別 –

+0

你應該逃避(以防止XSS攻擊)輸出不輸入 –

+0

會不會在數據庫中佔用更多內存? @Marktwigg –