doubi3996 2009-12-10 22:31
浏览 94
已采纳

在PHP中将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.

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?

  • 写回答

15条回答 默认 最新

  • dongzhuo1733 2010-04-02 00:32
    关注

    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 = Html2Text\Html2Text::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.

    Issues with other conversion scripts:

    • Since html2text (GPL) is not EPL-compatible.
    • lkessler's link (attribution) is incompatible with most open source licenses.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(14条)

报告相同问题?

悬赏问题

  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题