mb4it/htmlcleaner
Composer 安装命令:
composer require mb4it/htmlcleaner
包简介
Modern HTML cleaner with selectors and fluent API
README 文档
README
MB HTML Cleaner is a powerful PHP package for cleaning and transforming HTML content. It provides a fluent interface for manipulating HTML structures, removing unwanted elements and attributes, normalizing content, and performing various HTML transformations.
Installation
Install the package via Composer:
composer require mb4it/htmlcleaner
Basic Usage
use MB\Support\HtmlCleaner\HtmlCleaner; $cleaner = new HtmlCleaner(); $result = $cleaner->clean('<p><b>Hello</b> <i>world</i>!</p>');
HtmlCleaner Methods
stripEmptyTag()
RemoveTransformer empty elements (with no text content or only whitespace) matching the given tag names.
// RemoveTransformer empty p tags $result = $cleaner ->stripEmptyTag('p') ->clean($html); // RemoveTransformer all empty elements $result = $cleaner ->stripEmptyTag(null) ->clean($html);
HtmlCleaner Methods
transform()
Transform elements selected by the provided selectors using a TransformerInterface or closure. Supports CSS-like selectors for advanced element selection.
use MB\Support\HtmlCleaner\Selector\SelectorFacade; use MB\Support\HtmlCleaner\Transformer\ReplaceTransformer; // Basic tag selection $result = $cleaner ->transform( SelectorFacade::tag('span'), ReplaceTransformer::tag('b') ) ->clean($html); // CSS-like selector syntax $result = $cleaner ->transform( 'div.container > p', ReplaceTransformer::tag('div') ) ->clean($html); // Multiple selectors with comma (OR) $result = $cleaner ->transform( 'strong, span.bold', ReplaceTransformer::tag('b') ) ->clean($html); // Attribute selectors $result = $cleaner ->transform( 'a[href^="/"]', function ($node) { $node->setAttribute('target', '_blank'); } ) ->clean($html); // Pseudo-class selectors $result = $cleaner ->transform( 'div:has(p)', ReplaceTransformer::tag('section') ) ->clean($html); $result = $cleaner ->transform( 'span:has-text("Hello")', ReplaceTransformer::tag('b') ) ->clean($html); // Class and attribute combinations $result = $cleaner ->transform( 'div.highlight[data-type="article"]', ReplaceTransformer::tag('article') ) ->clean($html);
transformAnd()
Apply transformation when all specified selectors match (logical AND).
use MB\Support\HtmlCleaner\Selector\SelectorFacade; $result = $cleaner ->transformAnd([ SelectorFacade::tag('span'), SelectorFacade::style('font-weight', 'bold') ], new ReplaceTransformer('strong')) ->clean($html);
transformOr()
Apply transformation when any of the specified selectors match (logical OR).
$result = $cleaner ->transformOr([ SelectorFacade::tag('b'), SelectorFacade::tag('strong') ], new ReplaceTransformer('strong')) ->clean($html);
onlyText()
Keep only text content of specified tags, removing all child elements.
// Keep text content of span and p tags $result = $cleaner ->onlyText('span', 'p') ->clean($html); // Keep text content of all elements $result = $cleaner ->onlyText(null) ->clean($html);
drop()
RemoveTransformer entire elements matching the given tag names.
// RemoveTransformer script, style, and meta tags $result = $cleaner ->drop('script', 'style', 'meta') ->clean($html);
unwrap()
UnwrapTransformer elements (remove the tag but keep its children) matching the given tag names.
// RemoveTransformer html and body tags, keeping their content $result = $cleaner ->unwrap('span', 'font') ->clean($html);
wrap()
WrapTransformer elements matching the given tag names with a new tag.
// WrapTransformer all span elements with a div $result = $cleaner ->wrap('span', 'div') ->clean($html);
allowStyles()
Allow only specific inline styles on elements.
// Allow only background-color and color styles $result = $cleaner ->allowStyles('background-color', 'color') ->clean($html);
stripStyles()
Strip specific inline styles from all elements.
// RemoveTransformer background and margin styles $result = $cleaner ->stripStyles('background', 'margin') ->clean($html);
stripAttributes()
RemoveTransformer specified attributes from all elements.
// RemoveTransformer class, id, and onclick attributes $result = $cleaner ->stripAttributes('class', 'id', 'onclick') ->clean($html);
changeAttr()
Change attribute values for specific tags.
// Change href attribute of all a tags to "#" $result = $cleaner ->changeAttr('a', 'href', '#') ->clean($html); // Or $result = $cleaner ->changeAttr('a', ['href' => '#', 'target' => '_blank']) ->clean($html);
replaceTag()
ReplaceTransformer one tag with another, copying all attributes and styles.
// ReplaceTransformer span tags with div tags $result = $cleaner ->replaceTag('span', 'div') ->clean($html);
normalizeWhitespace()
Normalize whitespace within text nodes (collapse multiple spaces, trim).
$result = $cleaner ->normalizeWhitespace() ->clean($html);
outputFragment()
Set output mode to return only an HTML fragment (no , etc.).
$result = $cleaner ->outputFragment() ->clean($html);
outputDocument()
Set output mode to return a full HTML document.
$result = $cleaner ->outputDocument() ->clean($html);
clean()
Clean the provided HTML string according to the configured rules.
$result = $cleaner->clean($html);
Advanced Examples
HTML Document Transformation
$html = '<!DOCTYPE html><html dir="ltr"><head><meta charset="utf-8"><style>body{margin:0;}</style></head><body><p>Text <b>example</b></p></body></html>'; $result = $cleaner ->drop('style', 'meta', 'head') ->normalizeWhitespace() ->outputFragment() ->clean($html); // Result: <p>Text <b>example</b></p>
Style-Based Transformation
$html = '<span style="font-weight:bold">Hello</span><p>World</p>'; $result = $cleaner ->transform( SelectorFacade::style('font-weight', 'bold'), ReplaceTransformer::tag('b'), 10 ) ->stripStyles('font-weight') ->clean($html); // Result: <b>Hello</b><p>World</p>
Conditional Transformation with Closures
$html = '<span style="font-weight:bold">Hello</span><p>World</p>'; $result = $cleaner ->transform( SelectorFacade::style('font-weight', 'bold'), function (DomNode $node) { $doc = $node->ownerDocument; $parent = $node->parentNode; if (!$doc || !$parent) { return false; } $newNode = $doc->createElement('b'); $newNode->setAttribute('class', 'changed'); while ($node->firstChild) { $newNode->appendChild($node->firstChild); } $parent->replaceChild($newNode, $node); return true; } ) ->clean($html); // Result: <b class="changed">Hello</b><p>World</p>
mb4it/htmlcleaner 适用场景与选型建议
mb4it/htmlcleaner 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11 次下载、GitHub Stars 达 4, 最近一次更新时间为 2026 年 01 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「html」 「clean」 「proccess」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mb4it/htmlcleaner 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mb4it/htmlcleaner 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mb4it/htmlcleaner 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Texy converts plain text in easy to read Texy syntax into structurally valid (X)HTML. It supports adding of images, links, nested lists, tables and has full support for CSS. Texy supports hyphenation of long words (which reflects language rules), clickable emails and URL (emails are obfuscated again
Laravel console command for deleting temp files and associated model instances.
Removes invalid UTF-8 characters from the given text
Jobilla's general code styling rules for different tools
HTML and form generation
A modern HTML DOM parser for PHP using DOMDocument and Symfony CssSelector.
统计信息
- 总下载量: 11
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-15