Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Php Html Parser Laravel Package

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.

View on GitHub
Deep Wiki
Context7
## Getting Started
Install via Composer:
```bash
composer require paquettg/php-html-parser

The package provides a fluent API for parsing, manipulating, and generating HTML. Start with the Parser class for parsing HTML strings:

use Paquettg\HtmlParser\Parser;

$html = '<div class="example"><p>Hello <strong>World</strong></p></div>';
$dom = Parser::parse($html);

// Access nodes via DOM traversal
$div = $dom->getElementByTagName('div')->first();
$strong = $div->getElementByTagName('strong')->first();

$strong->setName('em'); // New in 1.2.0: Use setName() instead of deprecated methods
echo $div->saveHtml();

Key first-use cases:

  • HTML Parsing: Convert HTML strings to a traversable DOM structure.
  • Node Manipulation: Modify attributes, tags, or text content.
  • HTML Generation: Serialize modified DOM back to HTML.

Implementation Patterns

Common Workflows

  1. Parsing and Querying:

    $parser = new Parser();
    $dom = $parser->parse('<div id="content">...</div>');
    $nodes = $dom->getElementById('content');
    
  2. Attribute Manipulation:

    $node->setAttribute('class', 'updated');
    $node->removeAttribute('data-old');
    
  3. Text Extraction:

    $text = $node->getText(); // Get all text content
    $cleanText = $node->getInnerText(); // New in 1.2.0: Fixed null handling
    
  4. Encoding Handling (New in 1.2.0):

    use Paquettg\HtmlParser\Encoder\HtmlEntityEncoder;
    $encoder = new HtmlEntityEncoder();
    $node->setEncoder($encoder); // Custom encoding via EncoderInterface
    

Integration Tips

  • Laravel Blade: Use the parser in Blade directives for dynamic HTML generation:
    @php
        $html = Parser::parse($rawHtml)->saveHtml();
    @endphp
    {!! $html !!}
    
  • Form Handling: Sanitize user input before parsing:
    $cleanHtml = Parser::parse($userInput)->saveHtml();
    
  • Testing: Mock Parser for unit tests:
    $this->partialMock(Parser::class, ['parse']);
    

Gotchas and Tips

Breaking Changes (1.2.0)

  • Deprecated Methods: Avoid direct property access (e.g., $node->tag). Use:
    $node->getTag(); // Preferred in 1.2.0
    $node->setName('span'); // New method for tag name changes
    
  • Null Handling: mb_ereg_replace fixes in TextNode may affect edge cases with multibyte strings. Test with non-ASCII content.

Debugging

  • Node Traversal: Use saveHtml() to inspect modified DOM:
    echo $node->saveHtml(); // Debug rendered output
    
  • Encoding Issues: If special characters render incorrectly, explicitly set an encoder:
    $node->setEncoder(new HtmlEntityEncoder());
    

Extension Points

  • Custom Encoders: Implement EncoderInterface for custom HTML encoding:
    class MyEncoder implements EncoderInterface {
        public function encode($text) { ... }
    }
    
  • Parser Hooks: Extend Parser for pre/post-processing:
    $parser = new Parser();
    $parser->on('parse', function($dom) { /* Modify DOM */ });
    

Performance Tips

  • Batch Operations: Chain methods to avoid repeated DOM traversals:
    $dom->getElementByTagName('a')
        ->each(function($node) { $node->setAttribute('rel', 'nofollow'); });
    
  • Avoid Redundant Parsing: Cache parsed DOM objects if reusing HTML.

NO_UPDATE_NEEDED would **not** apply here due to the new features (e.g., `setName()`, `EncoderInterface`) and breaking changes. The above is the updated assessment.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
cadot.eu/make
besmartand-pro/php-quality-config
sentix/ai-chatbot
codifyo/ts-generator-bundle
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky