定制 cloudstudio/laravel-html-crawler 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

cloudstudio/laravel-html-crawler

Composer 安装命令:

composer require cloudstudio/laravel-html-crawler

包简介

A Laravel package for cleaning and transforming HTML content with a fluent interface

README 文档

README

A Laravel package for cleaning and transforming HTML content. It provides a fluent interface to remove unwanted elements like CSS, scripts, and more, with options to preserve specific elements and even convert the cleaned HTML to Markdown.

Features

  • Remove CSS (inline styles and <style> blocks)
  • Remove JavaScript (inline scripts and <script> blocks)
  • Preserve allowed tags through a configurable list or helper methods
  • Convert to Markdown for quick text transformations
  • Custom Regex Patterns to remove specific parts of the HTML
  • Whitespace Normalization with an option to preserve newlines

Installation

Install the package using Composer:

composer require cloudstudio/laravel-html-crawler

The package will automatically register itself in Laravel.

To publish the configuration file, run:

php artisan vendor:publish --provider="CloudStudio\HtmlCrawler\HtmlCrawlerServiceProvider"

Usage

1. Basic HTML Cleaning

By default, the package removes disallowed tags (for example, it will strip <div> tags and any tags not explicitly allowed):

use CloudStudio\HtmlCrawler\Facades\HtmlCrawler;

$html = '<div><p>Hello <strong>World</strong></p></div>';
$cleanHtml = HtmlCrawler::fromHtml($html)->clean();

// Expected output: "Hello World"

2. Preserving Allowed Tags

You can explicitly specify which tags to preserve:

Using setAllowedTags

use CloudStudio\HtmlCrawler\Facades\HtmlCrawler;

$html = '<div><p>Hello <a href="#">World</a></p></div>';
$cleanHtml = HtmlCrawler::fromHtml($html)
    ->setAllowedTags(['p', 'a'])
    ->clean();

// Expected output: '<p>Hello <a href="#">World</a></p>'

Using Helper Methods

The package offers helper methods to preserve groups of tags:

use CloudStudio\HtmlCrawler\Facades\HtmlCrawler;

$html = '<div><p>Hello <a href="#">World</a></p></div>';
$cleanHtml = HtmlCrawler::fromHtml($html)
    ->keepParagraphs()   // Preserves <p> tags
    ->keepLinks()        // Preserves <a> tags
    ->clean();

// Expected output: '<p>Hello <a href="#">World</a></p>'

3. Handling Scripts

Removing <script> by Default

By default, <script> blocks are removed:

use CloudStudio\HtmlCrawler\Facades\HtmlCrawler;

$html = '<div><script>alert("x")</script><p>Test</p></div>';
$cleanHtml = HtmlCrawler::fromHtml($html)->clean();

// Expected output: "Test"

Preserving <script> with keepScripts()

If you wish to keep <script> blocks, use the keepScripts() method:

use CloudStudio\HtmlCrawler\Facades\HtmlCrawler;

$html = '<div><script>alert("x")</script><p>Test</p></div>';
$cleanHtml = HtmlCrawler::fromHtml($html)
    ->keepScripts()
    ->clean();

// Expected output: '<script>alert("x")</script><p>Test</p>'

4. Handling CSS

By default, <style> blocks and CSS links are removed. To preserve them, use keepCss():

use CloudStudio\HtmlCrawler\Facades\HtmlCrawler;

$html = '<div><style>.text { color: red; }</style><p>Styled text</p></div>';
$cleanHtml = HtmlCrawler::fromHtml($html)
    ->keepCss()
    ->clean();

// Expected output: '<style>.text { color: red; }</style><p>Styled text</p>'

5. Using a Custom Regex Pattern

If you need to remove specific parts of the HTML using a regular expression:

use CloudStudio\HtmlCrawler\Facades\HtmlCrawler;

$html = '<div><span class="remove">Remove me</span><p>Keep me</p></div>';
$pattern = '/<span class="remove">.*?<\/span>/';
$cleanHtml = HtmlCrawler::fromHtml($html)
    ->useCustomPattern($pattern)
    ->clean();

// Expected output: '<p>Keep me</p>'

6. Converting to Markdown

You can convert the cleaned HTML to Markdown:

use CloudStudio\HtmlCrawler\Facades\HtmlCrawler;

$html = '<h1>Title</h1><p>Paragraph text</p>';
$markdown = HtmlCrawler::fromHtml($html)
    ->withMarkdown()
    ->clean();

7. Handling Newlines

Control how newlines are handled in the HTML:

use CloudStudio\HtmlCrawler\Facades\HtmlCrawler;

$html = "Line 1\nLine 2";
$cleanHtml = HtmlCrawler::fromHtml($html)
    ->preserveNewlines(false)  // Set to false to replace newlines with spaces
    ->clean();

// Expected output: "Line 1 Line 2"

8. Loading HTML from a URL

You can also load HTML directly from a URL:

use CloudStudio\HtmlCrawler\Facades\HtmlCrawler;

$cleanHtml = HtmlCrawler::fromUrl('https://example.com')
    ->clean();

// Output: the cleaned HTML content retrieved from the URL.

Configuration

The package includes a configuration file that allows you to define default options. After publishing the configuration file, you will find it at config/html-crawler.php:

return [
    'preserve_newlines'   => true,
    'allowed_tags'        => [],
    'convert_to_markdown' => false,
    'remove_scripts'      => true,
    'remove_styles'       => true,
];

You can modify these values according to your needs.

Troubleshooting

If you encounter the error:

BindingResolutionException: Target class [config] does not exist.

make sure your tests are running in a Laravel environment using orchestra/testbench. For package testing, install Testbench with:

composer require --dev orchestra/testbench

Then, set up your base test case to extend Testbench (see the package documentation for more details).

Testing

To run the tests, you can use:

./vendor/bin/pest

or if using PHPUnit:

./vendor/bin/phpunit

Changelog

Please see the CHANGELOG for detailed information on recent changes.

Contributing

Please refer to CONTRIBUTING for details on how to contribute to this package.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

This package is open-sourced software licensed under the MIT license.

cloudstudio/laravel-html-crawler 适用场景与选型建议

cloudstudio/laravel-html-crawler 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 25 次下载、GitHub Stars 达 2, 最近一次更新时间为 2025 年 02 月 16 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「markdown」 「html」 「crawler」 「laravel」 「cleaner」 「cloudstudio」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 cloudstudio/laravel-html-crawler 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 cloudstudio/laravel-html-crawler 我们能提供哪些服务?
定制开发 / 二次开发

基于 cloudstudio/laravel-html-crawler 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 25
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 20
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-02-16