如何使用PHP str_replace使用HTML标签替换htmlentities

Jee*_*eva 5 html php html-entities

我有如下内容.

我希望将<pre></pre>其转换为<p> </p>.但我无法实现它.以下是一个例子

$content = "<pre>This is a pre text &#13; I want to convert it to paragraphs ";

print_r(str_replace(array('<pre>', '</pre>', '&#13;'),array('<p>', '</p>', '<br/>'),htmlspecialchars($content)));
Run Code Online (Sandbox Code Playgroud)

但我得到了输出.有人可以帮我解决.提前致谢

小智 2

$content您在替换字符串之前 进行更改。

$content = "<pre>This is a pre text &#13; I want to convert it to paragraphs ";

print_r(htmlspecialchars($content));

// returns    &lt;pre&gt;This is a pre text &amp;#13; I want to convert it to paragraphs 
Run Code Online (Sandbox Code Playgroud)

没有一个符合你的str_replace

删除htmlspecialchars(),您将得到您想要的输出。

$content = "<pre>This is a pre text &#13; I want to convert it to paragraphs ";

print_r(str_replace(array('<pre>', '</pre>', '&#13;'),array('<p>', '</p>', '<br/>'),$content));

//returns    <p>This is a pre text <br/> I want to convert it to paragraphs 
Run Code Online (Sandbox Code Playgroud)