masroore / html2text
将HTML转换为纯文本格式的PHP包
0.0.1
2022-06-13 01:31 UTC
Requires
- php: ^8.0 || ^8.1
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.8
- nunomaduro/collision: ^6.0
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^7.0
- pestphp/pest: ^1.21
- pestphp/pest-plugin-laravel: ^1.1
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2024-09-13 07:01:45 UTC
README
概述
masroore/html2text是一个PHP包,可以将HTML页面转换为干净、易于阅读的纯ASCII文本。
安装
需要PHP 8.0+
您可以通过composer安装此包
composer require masroore/html2text
用法
从HTML中提取文本
use Kaiju\Html2Text\Html2Text; $converter = new Html2Text(); echo $converter->convert($html);
回调函数
您可以通过在预处理、标签替换和后处理中提供回调来更改格式化过程
# assign a pre-processing callback function. (transform href links) $converter->setPreProcessingCallback(fn (string $s) => preg_replace('%<\s*a[^>]*href=[\'"](.*?)[\'"][^>]*>([\s\S]*?)<\/\s*a\s*>%i', '$2 ($1)', $s)); # assign a tag-replacement callback function. (replace <li> tags) $converter->setTagReplacementCallback(fn (string $s) => preg_replace('/<\s*li[^>]*>/i', "\n- ", $s)); # post-processing hook $converter->setPostProcessingCallback(...); # process HTML echo $converter->convert($html);
测试
composer test
变更日志
请参阅变更日志了解最近更改的详细信息。
贡献
感谢您考虑为Html2Text做出贡献。所有贡献指南均在此处列出。
安全漏洞
请查阅我们的安全策略了解如何报告安全漏洞。
致谢
许可
Html2Text是一个开源软件,使用MIT许可证。