gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 16:39:00 24 4
gpt4 key购买 nike

我有如下内容。

我想要 <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)));

但我得到的是原样的输出。谁能帮我解决。提前致谢

最佳答案

您在替换字符串之前更改了 $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

没有一个与你的 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

关于php - 如何使用 PHP str_replace 使用 HTML 标签替换 htmlentities,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45182891/

24 4 0