PHP<;img>;str_replace中的函数不起作用


PHP <img> function in str_replace not working

我有这个PHP函数:

$test = "<b>Nice one</b><img src='https://i.stack.imgur.com/8hJby.jpg'/>";
$test = htmlspecialchars($test);
$test = str_replace("&lt;p&gt;", "<p>", $test);
$test = str_replace("&lt;a&gt;", "<a>", $test);
$test = str_replace("&lt;img&gt;", "<img>", $test);
$test = str_replace("&lt;b&gt;", "<b>", $test);
$test = str_replace("&lt;/b&gt;", "</b>", $test);
$test = str_replace("&lt;/img&gt;", "</img>", $test);
$test = str_replace("&lt;/a&gt;", "</a>", $test);
$test = str_replace("&lt;/p&gt;", "</p>", $test);
echo $test;

我试着运行代码。粗体文本的功能正在工作。但对于<img>,它没有显示图像,而是显示代码<img src='https://i.stack.imgur.com/8hJby.jpg'/>

问题,我如何设置图像显示?

感谢

一些行更改;

$test = str_replace("&lt;p&gt;", "<p>", $test);
$test = str_replace("&lt;a&gt;", "<a>", $test);
$test = str_replace("&lt;img", "<img", $test);
$test = str_replace("&lt;b&gt;", "<b>", $test);
$test = str_replace("&lt;/b&gt;", "</b>", $test);
$test = str_replace("/&gt;", "/>", $test);
$test = str_replace("&lt;/a&gt;", "</a>", $test);
$test = str_replace("&lt;/p&gt;", "</p>", $test);

尝试使用:

$test = html_entity_decode($test);

编辑:你试过在上面加页眉吗?

header('Content-Type: text/html; charset=utf-8');

&lt;img&gt;无法匹配代码中的img标记。在<img之后有一个空格,而不是&gt;