2014-11-01 62 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

回答