我有这个PHP函数:
$test = "<b>Nice one</b><img src='https://i.stack.imgur.com/8hJby.jpg'/>";
$test = htmlspecialchars($test);
$test = str_replace("<p>", "<p>", $test);
$test = str_replace("<a>", "<a>", $test);
$test = str_replace("<img>", "<img>", $test);
$test = str_replace("<b>", "<b>", $test);
$test = str_replace("</b>", "</b>", $test);
$test = str_replace("</img>", "</img>", $test);
$test = str_replace("</a>", "</a>", $test);
$test = str_replace("</p>", "</p>", $test);
echo $test;
我试着运行代码。粗体文本的功能正在工作。但对于<img>
,它没有显示图像,而是显示代码<img src='https://i.stack.imgur.com/8hJby.jpg'/>
问题,我如何设置图像显示?
感谢
一些行更改;
$test = str_replace("<p>", "<p>", $test);
$test = str_replace("<a>", "<a>", $test);
$test = str_replace("<img", "<img", $test);
$test = str_replace("<b>", "<b>", $test);
$test = str_replace("</b>", "</b>", $test);
$test = str_replace("/>", "/>", $test);
$test = str_replace("</a>", "</a>", $test);
$test = str_replace("</p>", "</p>", $test);
尝试使用:
$test = html_entity_decode($test);
编辑:你试过在上面加页眉吗?
header('Content-Type: text/html; charset=utf-8');
<img>
无法匹配代码中的img标记。在<img
之后有一个空格,而不是>