将 HTML 转换为 PHP 中的纯文本以用于电子邮件 [英] Converting HTML to plain text in PHP for e-mail
问题描述
我使用 TinyMCE 来允许我的网站内的文本格式最少.从生成的 HTML 中,我想将其转换为电子邮件的纯文本.我一直在使用一个名为 html2text 的类,但它确实缺乏对 UTF-8 的支持,其中其他事情.然而,我确实喜欢它将某些 HTML 标签映射到纯文本格式——比如在以前带有 <i> 的文本周围放置下划线.HTML 中的标签.
I use TinyMCE to allow minimal formatting of text within my site. From the HTML that's produced, I'd like to convert it to plain text for e-mail. I've been using a class called html2text, but it's really lacking in UTF-8 support, among other things. I do, however, like that it maps certain HTML tags to plain text formatting — like putting underscores around text that previously had <i> tags in the HTML.
有没有人使用类似的方法在 PHP 中将 HTML 转换为纯文本?如果是这样:您是否推荐我可以使用的任何第三方类?或者你如何最好地解决这个问题?
Does anyone use a similar approach to converting HTML to plain text in PHP? And if so: Do you recommend any third-party classes that I can use? Or how do you best tackle this issue?
推荐答案
使用 html2text(示例
Use html2text (example HTML to text), licensed under the Eclipse Public License. It uses PHP's DOM methods to load from HTML, and then iterates over the resulting DOM to extract plain text. Usage:
// when installed using the Composer package
$text = Html2TextHtml2Text::convert($html);
// usage when installed using html2text.php
require('html2text.php');
$text = convert_html_to_text($html);
虽然不完整,但它是开源的,欢迎贡献.
Although incomplete, it is open source and contributions are welcome.
其他转换脚本的问题:
- 因为 html2text (GPL) 与 EPL 不兼容.
- lkessler 的链接(署名)与大多数开源许可证不兼容.
- Since html2text (GPL) is not EPL-compatible.
- lkessler's link (attribution) is incompatible with most open source licenses.
这篇关于将 HTML 转换为 PHP 中的纯文本以用于电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!