如何让文本像原始输入一样显示在文本区域


How to get text to display like orginal input from text area

我一直在尝试格式化从文本区域获得的字符串。我希望它显示的外观像它的原始文本区输入。你会看到我使用了很多功能,所以,如果使用不重要,请建议我。

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> 

添加

$detail = htmlspecialchars($detail);

对于新行,请使用PHP_EOL变量,或将<br>替换为'n

http://php.net/manual/en/function.htmlspecialchars.php