alexanderpavlov / html2text
一个PHP脚本,用于将HTML转换为纯文本格式
1.1.2
2021-09-10 07:49 UTC
Requires
- php: >=7.0
- ext-dom: *
- ext-libxml: *
Requires (Dev)
- phpunit/phpunit: ~7.0
- soundasleep/component-tests: ~0.2
README
html2text是一个非常简单的脚本,使用DOM方法将HTML转换为类似浏览器渲染的格式 - 完美适合需要快速文本表示的地方。例如
<html> <title>Ignored Title</title> <body> <h1>Hello, World!</h1> <p>This is some e-mail content. Even though it has whitespace and newlines, the e-mail converter will handle it correctly. <p>Even mismatched tags.</p> <div>A div</div> <div>Another div</div> <div>A div<div>within a div</div></div> <a href="http://foo.com">A link</a> </body> </html>
将被转换为
Hello, World!
This is some e-mail content. Even though it has whitespace and newlines, the e-mail converter will handle it correctly.
Even mismatched tags.
A div
Another div
A div
within a div
[A link](http://foo.com)
查看原始博客文章或相关的StackOverflow回答。
安装
{ "require": { "soundasleep/html2text": "~1.1" } }
然后可以非常简单地使用它
$text = \Soundasleep\Html2Text::convert($html);
您也可以包含提供的html2text.php
并使用$text = convert_html_to_text($html);
代替。
选项
将选项作为convert
的第二个参数传递,例如
$options = array( 'ignore_errors' => true, // other options go here ); $text = \Soundasleep\Html2Text::convert($html, $options);
测试
在tests/
目录中提供了一些非常基本的测试。运行它们使用composer install && vendor/bin/phpunit
。
故障排除
未找到类'DOMDocument'
您需要为您的PHP版本安装PHP XML扩展。例如,apt-get install php7.1-xml
许可证
html2text
根据MIT许可证授权,使其适用于Eclipse和GPL项目。
其他版本
还可以查看html2text_ruby,一个Ruby实现。