sqginfo/html2text

将HTML转换为纯文本格式的PHP脚本

安装: 478

依赖: 1

建议者: 0

安全性: 0

星标: 0

关注者: 3

分支: 135

语言:HTML

0.3.4.1 2016-12-05 23:50 UTC

This package is auto-updated.

Last update: 2024-09-11 00:03:22 UTC


README

html2text是一个非常简单的脚本,它使用PHP的DOM方法从HTML中加载,然后迭代结果DOM以正确输出纯文本。例如

<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回答

安装

您可以使用Composer添加到您的项目中

{
  "require": {
    "soundasleep/html2text": "~0.3"
  }
}

然后非常简单地使用它

$text = Html2Text\Html2Text::convert($html);

您还可以包含提供的html2text.php文件,并使用$text = convert_html_to_text($html);代替。

测试

tests/目录中提供了几个非常基本的测试。使用composer install --dev && vendor/bin/phpunit运行它们。

许可证

html2textEPL v1.0LGPL v3.0下双重授权,使其适用于Eclipse和GPL项目。

其他版本

还可以查看html2text_ruby,这是一个Ruby实现。