usox/html2text

一个用于将HTML转换为纯文本格式的hacklang脚本

v1.0.0 2019-02-27 11:23 UTC

This package is auto-updated.

Last update: 2024-08-28 00:12:44 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)

安装

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

{
  "require": {
    "usox/html2text": "^1"
  }
}

然后非常简单地使用它

$converter = new \Usox\Html2Text()
$text = $converter->convert($html);

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

选项

将选项作为convert的第二个参数传递,例如

$html = 'some fine html';
$options = dict[
  'ignore_errors' => true,
  // other options go here
];
$converter = new \Usox\Html2Text()
echo $converter->convert($html, $options);

测试

tests/目录中提供了一些非常基本的测试。运行它们使用composer install && vendor/bin/hacktest tests

许可证

html2textMIT许可证的约束,这使得它适用于Eclipse和GPL项目。

其他版本

这是一个从这里找到的php版本的移植。