devanoxltd/php-html-parser
Lightweight PHP HTML parser with jQuery-style CSS selectors. Load HTML from strings or files, handle imperfect markup, and traverse nodes (attributes, innerHtml, siblings). Ideal for quick web scraping and DOM extraction in PHP 7.2–7.4.
Architecture Fit
The paquettg/php-html-parser package (now maintained as devanoxltd/php-html-parser) is a lightweight HTML parsing library for PHP/Laravel, offering DOM manipulation capabilities without bloating the stack with heavy frameworks like DOMDocument. Its event-driven parsing model and Laravel-friendly design (e.g., PSR-compliant interfaces, type hints) align well with modern Laravel applications requiring HTML scraping, sanitization, or dynamic content generation. The 1.2.0 release introduces minor but meaningful improvements:
ReturnTypeWillChange, offsetGet improvements) align with PHP 8.x+ best practices, easing adoption in strict Laravel projects.Integration Feasibility
setName method, EncoderInterface) are additive and can be adopted incrementally.AppServiceProvider).Technical Risk
EncoderInterface provides a clear migration path.mb_ereg_replace null handling (PR #260) suggests prior multibyte encoding fragility; test thoroughly in UTF-8-heavy environments.innerText clearing (PR #324) could affect cached or serialized HtmlNode instances if not handled in custom logic.Key Questions
EncoderInterface to avoid runtime errors.ReturnTypeWillChange attribute is a non-issue, but type hints may require polyfills.object check for tag) meet security/compliance needs? Consider pairing with Laravel’s Str::of() for sanitization.Stack Fit
HtmlParser::class => fn() => new Parser()).@parseHtml).EncoderInterface aligns with Symfony’s Stringable/Encoder patterns.HtmlNode/Tag classes easily with PHPUnit’s type system.Migration Path
composer require devanoxltd/php-html-parser:^1.2.0
EncoderInterface and rebind to the container:
$this->app->bind(EncoderInterface::class, CustomEncoder::class);
mb_ereg_replace fixes).HtmlNode instances (check innerText persistence).Compatibility
setName) are optional; existing code remains unchanged.Sequencing
config('features.html_parser_v2', false) to toggle new methods (e.g., setName) gradually.EncoderInterface enforcement or method deprecations.Maintenance
ReturnTypeWillChange may evolve).Support
EncoderInterface usage; check for mbstring extension (required for multibyte support).strict_types=1 in composer.json.Scaling
Parser instances or use Laravel’s cache layer for HtmlNode results.Failure Modes
| Risk | Mitigation Strategy | Laravel Integration Point |
|---|---|---|
| Malformed HTML crashes | Wrap parsing in try-catch; log errors. |
Use Laravel’s Log::error(). |
| Multibyte encoding corruption | Validate input with mb_check_encoding(). |
Add middleware for API inputs. |
| Custom encoder misconfig | Unit test all encoder implementations. | Use Laravel’s phpunit.xml setup. |
| Serialization conflicts | Avoid serializing HtmlNode; use DTOs. |
Implement Arrayable/Jsonable. |
Ramp-Up
Parser::loadHTML(), Tag::setName(), and EncoderInterface.Blade::directive('parse', fn($expr) => "<?php echo app(\\Devanox\\HtmlParser\\Parser::class)->loadHTML($expr)->saveHTML(); ?>");
EncoderInterface compliance:
$this->assertInstanceOf(EncoderInterface::class, $parser->getEncoder());
setName, getTag()) and type safety for teams upgrading from PHP 7.x.How can I help you explore Laravel packages today?