2014-11-01 65 views
0

我一直在试图从文本区域获取格式化字符串。我希望它显示从文本区域输入看起来像原来的。你会看到我已经使用了很多功能,如果使用无关紧要,请告诉我。如何从文本区域获取文本以显示原始输入

从文本区PHP

$detail = nl2br(addslashes($_POST['detail'])); 
$detail = trim($detail); 
$detail = str_replace("\r\n","",$detail); 
//remove html tags except br prevent it show as html style 
$detail = strip_tags($detail, '<br>'); 
// don't allow consequent new line 
$detail = preg_replace('#(<br */?>\s*){2,}#i', '<br /><br />', $detail); 

例输入字符串,我希望它表明这样当我echo $detail

#include <iostream> 

using namesspace std; 

int main(){ 
return 0; 
} 

The most tragic<br /> 
<h1> thing in the world</h1> 

is a man of genius who is not a man of honor. 

但下面的结果我有,我知道这是因为我使用strip_tags。现在,我不知道请帮助。你会看到一些文字消失,<iostream><br /><h1>等。

#include 

using namesspace std; 

int main(){ 
return 0; 
} 

The most tragic 

thing in the world 

is a man of genius who is not a man of honor. 

另一个例子,如果这是一个输入,它应该显示相同。但对于我的代码,没有任何显示。

<textarea rows="7" cols="50" onkeyPress="return addP(event,this)"></textarea> 
+2

尝试添加$细节=用htmlspecialchars($细节); – 2014-11-01 08:51:43

+0

它似乎工作,但真正的新行也取代了。他们显示在单行中。 – Marcus 2014-11-01 09:01:50

+1

也尝试使用'nl2br()'。 – 2014-11-01 09:03:53

回答