2016-08-12 63 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 –