如何在 PHP 中删除 Html 标签?

发布于 2024-08-25 19:36:16 字数 319 浏览 5 评论 0原文

我在字符串中使用 htmlspecialchars 函数。但是,我不想清理它们;

<b>, <br>, <p> <ul>,<li> bla bla...

示例:Mystring = "
test

aaaa

";

我想要; =

<script>.....</script>

I use htmlspecialchars function in my string. But, I don't want to clean them;

<b>, <br>, <p> <ul>,<li> bla bla...

Example: Mystring = "<script>.....</script><br><b>test</b><p>aaaa</p>";

I want to; =

<script>.....</script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你丑哭了我 2024-09-01 19:36:16

看看 HTML Purifier,尤其是 白名单功能。

如果您允许 HTML 标签,这可能是最安全的方法。您可以在此处查看比较。

Have a look at HTML Purifier, and especially the whitelist feature.

This is probably the safest approach if you allow HTML tags. You can view the comparison here.

<逆流佳人身旁 2024-09-01 19:36:16

您想删除所有标签吗?使用 strip_tags()

You want to remove all tags? Use strip_tags().

西瓜 2024-09-01 19:36:16

您可以使用 HTML Sanitizer 类 - http://www.phpclasses.org/browse/package /3746.html

You can use HTML Sanitizer Class - http://www.phpclasses.org/browse/package/3746.html

贩梦商人 2024-09-01 19:36:16

您可以使用内置 PHP 函数strip_tags($text, '

  • ');
  • 您不'不需要使用任何类或外部库来从用户输入中删除 HTML 标签。

    在上面的示例中,我清除了除

  • ; 之外的所有标记中的 $text 。
  • 此 PHP 函数将清理用户输入,确保不会提交

    You can use built in PHP function: strip_tags($text, '<p><a><li><b>');

    You don't need to use any class or external library, to remove HTML tags from user input.

    In the above example I am cleaning the $text from all tags except <p><a><li><b>;

    This PHP function will clean user input making sure are not submitting <script> or </div> to break your page.

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文