p3k/html-sanitizer

一个具有良好默认设置的HTML净化器

0.1.2 2022-11-19 16:03 UTC

This package is auto-updated.

Last update: 2024-09-19 20:33:51 UTC


README

一个具有良好默认设置的HTML净化器,可用于在应用程序中显示不受信任的HTML。

仅允许基本格式化标签集,删除所有<script>标签。删除允许标签的所有属性,除了在Microformats 2类中留下。

安装

composer require p3k/html-sanitizer

使用方法

$output = p3k\HTML::sanitize($input);

选项

您可以向sanitize函数传递的最小数量选项

$options = [
  'baseURL' => 'https://example.com/'
];

$output = p3k\HTML::sanitize($input, $options);
  • baseURL - (默认 false)
  • allowImg - (true/false, 默认 true) - 是否允许输出中的img标签
  • allowMf2 - (true/false, 默认 true) - 是否允许元素上的Microformats 2类
  • allowTables - (true/false, 默认 false) - 是否允许表格元素(tabletheadtbodytrtd

允许的标签

以下HTML标签是输入中唯一允许的标签。其他所有内容都将被删除。

  • a
  • abbr
  • b
  • br
  • code
  • del
  • em
  • i
  • q
  • strike
  • strong
  • time
  • blockquote
  • pre
  • p
  • h1
  • h2
  • h3
  • h4
  • h5
  • h6
  • ul
  • li
  • ol
  • span
  • hr
  • img - 仅当$options['allowImg']true
  • tabletheadtbodytfoottrthtd - 仅当$options['allowTables']true

以下属性以外的所有属性都将被删除。

  • <a> - href
  • <img> - src width height alt
  • <time> - datetime

如果$options['allowMf2']true,则将删除class属性,但保留Microformats 2类值。

例如

<h2 class="p-name name">Hello</h2>

将变成

<h2 class="p-name">Hello</h2>