fglueck/html2text

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

安装: 1

依赖: 0

建议: 0

安全性: 0

星标: 0

关注者: 0

分支: 135

开放问题: 0

语言:HTML

1.1.0 2019-02-15 01:44 UTC

This package is auto-updated.

Last update: 2024-09-06 17:11:22 UTC


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

安装

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

{
  "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

许可证

html2textMIT许可证下发布,适用于Eclipse和GPL项目。

其他版本

还可以查看html2text_ruby,Ruby实现。