简体   繁体   English

如何仅为论坛呈现某些html标签?

[英]How can I only render certain html tags for my forum?

I am currently building a forum and am trying to print out posts with formatting (bold, italicized, images, etc.) The user inputs the text using http://summernote.org/ My php code is currently set to: 我目前正在建立一个论坛,并尝试打印具有格式(粗体,斜体,图像等)的帖子。用户使用http://summernote.org/输入文本。我的php代码当前设置为:

echo nl2br(e($content));

Which prints out: (printing the actual tags) 哪个打印出来:(打印实际标签)

<p><b>asdffddd d d d d </b></p>

When I surround my echo function with htmlspecialchars_decode, it renders every single html tag. 当我用htmlspecialchars_decode包围我的echo函数时,它将呈现每个html标签。 This is obviously not ideal, because users could put things like iframe's or div's and break the layout of my page. 这显然是不理想的,因为用户可以放置iframe或div之类的东西并破坏页面的布局。

What would be the best way to only render the following tags 仅呈现以下标签的最佳方法是什么

<b>
<i>
<u>
<p>
<h1-6>
<img>
<a>
<span>
<ul>
<ol>
<li>

I would still like it to display non-allowed tags in text form, just not render them. 我仍然希望它以文本形式显示不允许的标签,只是不渲染它们。

Use strip_tags to achieve this. 使用strip_tags来实现。

<?php
$html = 'Your html';
$allowableTags = '<a><b>'; // add allowable tags
echo strip_tags($html,$allowableTags);
?>

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM