michalskoula/remove-formatting

一个简单的工具,用于删除(某些)HTML格式,例如从Microsoft Word或Google Docs复制的文本。

1.0.1 2024-09-17 16:51 UTC

This package is auto-updated.

Last update: 2024-09-17 16:51:55 UTC


README

一个简单的工具,用于删除(某些)HTML格式,例如从Microsoft Word或Google Docs复制的文本。它删除不需要的样式、类、元素等。

功能

  • 删除不需要的元素(默认:保留 ['a', 'img', 'p', 'strong'])
  • 删除不需要的属性(默认:保留 ['href', 'src'])
  • 删除不需要的空白字符(默认:false)

安装

composer require michalskoula/remove-formatting

需要PHP 7.4+

用法

use MichalSkoula\RemoveFormatting\Remover;
use MichalSkoula\RemoveFormatting\RemoverOptions;

// create options object (optional)
$options = new RemoverOptions();

// add <u> to the default list of allowed elements
$options->addAllowedElement('u');

// add class to the default list of allowed attributes
$options->addAllowedAttribute('class');

// create Remover object
$remover = new Remover($options);

$dirtyHtml = 'some dirty <span>spans</span> ...';

// run it and get clean html with only allowed elements and attributes
$cleanHtml = $remover->remove($dirtyHtml);

查看example.php

HTML之前

<p style="margin-bottom:11px"><span style="font-size:11pt"><span style="line-height:107%"><span style="font-family:&quot;Calibri&quot;,sans-serif">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. 
Phasellus rhoncus. Quisque porta. Maecenas libero. Sed vel lectus. Donec odio tempus molestie, porttitor ut, iaculis quis, sem. <u>Integer tempor</u>. Fusce wisi. </span></span></span></p>
<p style="margin-bottom:11px"><span style="font-size:11pt"><span style="line-height:107%"><span style="font-family:&quot;Calibri&quot;,sans-serif">
<img height="1280" src="https://skoula.cz/nazdar/screens/nazdar1.png" width="1277" /></span></span></span></p>
<p style="margin-bottom:11px"><span style="font-size:11pt"><span style="line-height:107%"><span style="font-family:&quot;Calibri&quot;,sans-serif">Nullam at arcu a <b>est sollicitudin euismod</b>. 
Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos. </span></span></span></p>
<p style="margin-bottom:11px"><span style="font-size:11pt"><span style="line-height:107%"><span style="font-family:&quot;Calibri&quot;,sans-serif">Sed ac dolor sit 
<a href="https://skoula.cz/nazdar" style="color:#0563c1; text-decoration:underline">amet purus malesuada</a> congue. Etiam dictum tincidunt diam. Aenean vel massa quis mauris vehicula lacinia.</span></span></span></p>

HTML之后

<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Phasellus rhoncus. Quisque porta. Maecenas libero. Sed vel lectus. Donec odio tempus molestie, porttitor ut, iaculis quis, sem. <u>Integer tempor</u>. Fusce wisi. </p>

<p><img src="https://skoula.cz/nazdar/screens/nazdar1.png"></p>

<p>Nullam at arcu a est sollicitudin euismod.Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos.</p>

<p>Sed ac dolor sit <a href="https://skoula.cz/nazdar">amet purus malesuada</a> congue. Etiam dictum tincidunt diam. Aenean vel massa quis mauris vehicula lacinia.</p>

关于