我希望你能像往常一样帮助你,谢谢。
我有一个在textarea中输入信息的表单,我让用户编写html标签,因为有时候他们想要文字写html标签,所以如果他们写这样的东西作为输入:
<h1>This is the input content</h1>
I like Pizza
I like Cheese
This is the end of the input, as you see I pressed enter several times
我使用nl2br保存输入,如下所示:
$textForDataBase = nl2br($_POST['input']);
然后数据库值是这样的:
<h1>This is the input content</h1>
I like Pizza<br />I like Cheese<br />This is the end of the input, as you see I pressed enter several times
然后在显示内容之前,我将htmlspecialchars
应用于我的字符串,但我希望所有不是断行的html标签都转换为html实体,所以我尝试使用preg_replace但是我有没有运气,我该怎么办?
$html = htmlspecialchars($val['descripcion']);
echo preg_replace('#<br\s*/?>#i', "\n", $html);
如果我不使用htmlspecialchars,输出将是这样的:
但如果我使用它:
<h1>This is the input content</h1>
I like Pizza<br />I like Cheese<br />This is the end of the input, as you see I pressed enter several times
预期输出为:
<h1>This is the input content</h1>
I like Pizza
I like Cheese
This is the end of the input, as you see I pressed enter several times
答案 0 :(得分:1)
当您在db中存储输入详细信息时删除nl2br并将用户详细信息存储为与输入值相同,然后当u显示值首先使用htmlspecialchars然后使用nl2br并最后回显它。
就这样改变。
$textForDataBase = $_POST['input'];
$html = htmlspecialchars($val['descripcion']);
echo nl2br($html);
答案 1 :(得分:0)
也许您可以使用strip_tags();
<?php
$string = "
<p>This is a p element</p><a href=''>An a element</a><h1>This is the input content</h1> I like Pizza<br />I like Cheese<br />This is the end of the input, as you see I pressed enter several times";
$output = strip_tags($string,"<br>");
echo $output;