masterminds/html5
Standards-compliant HTML5 parser and serializer for PHP. Provides SAX-like event parsing, DOM tree building, namespace support, Composer install, and interoperability with QueryPath. Stable and widely used in production.
Install via Composer: composer require masterminds/html5. The primary entry point is the Masterminds\HTML5 class. Start by parsing a fragment or full HTML document into a DOMDocument using loadHTML($html), then serialize back with saveHTML($dom) or save($dom, 'file.html'). For typical use cases (e.g., sanitizing user input, transforming HTML, preprocessing templates), this high-level API is sufficient—no need to touch lower-level parsers or events unless building a custom tool.
<script>, on* handlers) before output.EventHandlerInterface for SAX-like streaming parsing (e.g., content extraction, analytics, or SEO meta extraction) without building a full DOM.xmlNamespaces: true and/or implicitNamespaces for SVG/MathML embedded in HTML (e.g., rendering dynamic charts or diagrams).OutputRules for tailored output (e.g., minification, pretty-printing with custom indentation, or canonicalization).:, .). Namespaced attributes must be declared explicitly or via implicitNamespaces—otherwise they’re silently dropped.<td>, <tr>, <th>, <tbody>, etc.) respect HTML5’s optional end tags, which affects DOM structure—be cautious when serializing to match original source.<svg>/<math> tags are serialized as void (<svg/>) unless they have content (2.10.0+). Use innerHTML-style logic carefully if relying on empty-string children.encode_entities: true only when required (e.g., for email templates), as it aggressively encodes characters (e.g., é → é). Default encodes only what’s necessary.ext-libxml and ctype dependencies—ensure no legacy assumptions about those in your codebase. Works across PHP 5.3+, but latest releases target PHP 8.0+.HTML5 once and reuse it—costly setup (tokenization tables, rules) occurs on construction. Prefetch loadHTML()/saveHTML() for predictable latency.How can I help you explore Laravel packages today?