이메일 용 PHP에서 HTML을 일반 텍스트로 변환 저는 TinyMCE 를 사용 하여 내 사이트

저는 TinyMCE 를 사용 하여 내 사이트 내에서 최소한의 텍스트 서식을 허용합니다. 생성 된 HTML에서 전자 메일 용 일반 텍스트로 변환하고 싶습니다. 나는 html2text 라는 클래스를 사용해 왔지만 다른 것들 중에서도 UTF-8 지원이 정말 부족합니다. 그러나 저는 특정 HTML 태그를 일반 텍스트 형식으로 매핑합니다. 예를 들어 이전에 HTML에서 <i> 태그가 있었던 텍스트 주위에 밑줄을 넣는 것과 같습니다.

누구든지 PHP에서 HTML을 일반 텍스트로 변환하는 유사한 접근 방식을 사용합니까? 그렇다면 : 내가 사용할 수있는 타사 클래스를 추천합니까? 아니면이 문제를 어떻게 가장 잘 해결합니까?



답변

Eclipse Public License에 따라 라이센스가 부여 된 html2text (예제 HTML to text ) 를 사용하십시오 . PHP의 DOM 메서드를 사용하여 HTML에서로드 한 다음 결과 DOM을 반복하여 일반 텍스트를 추출합니다. 용법:

// 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);

불완전하지만 오픈 소스이며 기여를 환영합니다.

다른 변환 스크립트 문제 :

  • 이후 html2text (GPL)는 EPL-호환되지 않습니다.
  • lkessler의 링크 (속성)는 대부분의 오픈 소스 라이선스와 호환되지 않습니다.

답변

여기 또 다른 해결책이 있습니다.

$cleaner_input = strip_tags($text);

살균 기능의 다른 변형은 다음을 참조하십시오.

https://github.com/ttodua/useful-php-scripts/blob/master/filter-php-variable-sanitize.php


답변

DOMDocument를 사용하여 HTML에서 텍스트로 변환 하는 것은 실행 가능한 솔루션입니다. PHP5가 필요한 HTML2Text를 고려하십시오.

UTF-8과 관련하여 “howto”페이지의 글에는 다음과 같은 내용이 있습니다.

유니 코드에 대한 PHP의 자체 지원은 매우 열악하며 항상 utf-8을 올바르게 처리하지는 않습니다. html2text 스크립트는 mbstring 모듈이 필요없는 유니 코드 안전 메서드를 사용하지만 항상 PHP의 자체 인코딩 처리에 대처할 수는 없습니다. PHP는 utf-8과 같은 유니코 드나 인코딩을 실제로 이해하지 못하며 시스템의 기본 인코딩을 사용합니다. 이는 ISO-8859 제품군 중 하나 인 경향이 있습니다. 결과적으로 텍스트 편집기에서 utf-8 또는 단일 바이트로 표시되는 유효한 문자가 PHP에 의해 잘못 해석 될 수 있습니다. 따라서 html2text에 유효한 문자를 제공한다고 생각하더라도 그렇지 않을 수 있습니다.

저자는이를 해결하기위한 몇 가지 접근 방식을 제공하고 HTML2Text 버전 2 (DOMDocument 사용)가 UTF-8을 지원한다고 설명합니다.

상업적 사용에 대한 제한 사항에 유의하십시오.


답변

신뢰할 수있는 strip_tags 기능이 있습니다. 그래도 예쁘지 않습니다. 소독 할뿐입니다. 멋진 밑줄을 얻기 위해 문자열 바꾸기와 결합 할 수 있습니다.


<?php
// to strip all tags and wrap italics with underscore
strip_tags(str_replace(array("<i>", "</i>"), array("_", "_"), $text));

// to preserve anchors...
str_replace("|a", "<a", strip_tags(str_replace("<a", "|a", $text)));

?>

답변

-stdin 및 -dump 옵션과 함께 lynx를 사용하여 다음을 수행 할 수 있습니다.

<?php
$descriptorspec = array(
   0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
   1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
   2 => array("file", "/tmp/htmp2txt.log", "a") // stderr is a file to write to
);

$process = proc_open('lynx -stdin -dump 2>&1', $descriptorspec, $pipes, '/tmp', NULL);

if (is_resource($process)) {
    // $pipes now looks like this:
    // 0 => writeable handle connected to child stdin
    // 1 => readable handle connected to child stdout
    // Any error output will be appended to htmp2txt.log

    $stdin = $pipes[0];
    fwrite($stdin,  <<<'EOT'
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
 <title>TEST</title>
</head>
<body>
<h1><span>Lorem Ipsum</span></h1>

<h4>"Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit..."</h4>
<h5>"There is no one who loves pain itself, who seeks after it and wants to have it, simply because it is pain..."</h5>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque et sapien ut erat porttitor suscipit id nec dui. Nam rhoncus mauris ac dui tristique bibendum. Aliquam molestie placerat gravida. Duis vitae tortor gravida libero semper cursus eu ut tortor. Nunc id orci orci. Suspendisse potenti. Phasellus vehicula leo sed erat rutrum sed blandit purus convallis.
</p>
<p>
Aliquam feugiat, neque a tempus rhoncus, neque dolor vulputate eros, non pellentesque elit lacus ut nunc. Pellentesque vel purus libero, ultrices condimentum lorem. Nam dictum faucibus mollis. Praesent adipiscing nunc sed dui ultricies molestie. Quisque facilisis purus quis felis molestie ut accumsan felis ultricies. Curabitur euismod est id est pretium accumsan. Praesent a mi in dolor feugiat vehicula quis at elit. Mauris lacus mauris, laoreet non molestie nec, adipiscing a nulla. Nullam rutrum, libero id pellentesque tempus, erat nibh ornare dolor, id accumsan est risus at leo. In convallis felis at eros condimentum adipiscing aliquam nisi faucibus. Integer arcu ligula, porttitor in fermentum vitae, lacinia nec dui.
</p>
</body>
</html>
EOT
    );
    fclose($stdin);

    echo stream_get_contents($pipes[1]);
    fclose($pipes[1]);

    // It is important that you close any pipes before calling
    // proc_close in order to avoid a deadlock
    $return_value = proc_close($process);

    echo "command returned $return_value\n";
}

답변

이 기능을 테스트 할 수 있습니다.

function html2text($Document) {
    $Rules = array ('@<script[^>]*?>.*?</script>@si',
                    '@<[\/\!]*?[^<>]*?>@si',
                    '@([\r\n])[\s]+@',
                    '@&(quot|#34);@i',
                    '@&(amp|#38);@i',
                    '@&(lt|#60);@i',
                    '@&(gt|#62);@i',
                    '@&(nbsp|#160);@i',
                    '@&(iexcl|#161);@i',
                    '@&(cent|#162);@i',
                    '@&(pound|#163);@i',
                    '@&(copy|#169);@i',
                    '@&(reg|#174);@i',
                    '@&#(d+);@e'
             );
    $Replace = array ('',
                      '',
                      '',
                      '',
                      '&',
                      '<',
                      '>',
                      ' ',
                      chr(161),
                      chr(162),
                      chr(163),
                      chr(169),
                      chr(174),
                      'chr()'
                );
  return preg_replace($Rules, $Replace, $Document);
}

답변

간단한 HTML 이메일과 간단한 일반 텍스트 파일에 적합한 기존 솔루션을 찾지 못했습니다.

이 저장소를 열었습니다. 누군가에게 도움이되기를 바랍니다. 그런데 MIT 라이센스 🙂

https://github.com/RobQuistNL/SimpleHtmlToText

예:

$myHtml = '<b>This is HTML</b><h1>Header</h1><br/><br/>Newlines';
echo (new Parser())->parseString($myHtml);

보고:

**This is HTML**
### Header ###


Newlines