Hướng dẫn php convert html to text - php chuyển đổi html thành văn bản

88

Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.

Tôi sử dụng tinymce để cho phép định dạng văn bản tối thiểu trong trang web của mình. Từ HTML được sản xuất, tôi muốn chuyển đổi nó thành văn bản đơn giản cho e-mail. Tôi đã sử dụng một lớp có tên HTML2TEXT, nhưng nó thực sự thiếu hỗ trợ UTF-8, trong số những thứ khác. Tuy nhiên, tôi làm như vậy, nó ánh xạ một số thẻ HTML thành định dạng văn bản đơn giản - như đặt dấu gạch dưới xung quanh văn bản mà trước đây có thẻ trong HTML.

Có ai sử dụng một cách tiếp cận tương tự để chuyển đổi HTML thành văn bản đơn giản trong PHP không? Và nếu vậy: Bạn có đề xuất bất kỳ lớp bên thứ ba nào tôi có thể sử dụng không? Hoặc làm thế nào để bạn giải quyết tốt nhất vấn đề này?

Hướng dẫn php convert html to text - php chuyển đổi html thành văn bản

Benmorel

32,8K48 Huy hiệu vàng170 Huy hiệu bạc303 Huy hiệu đồng48 gold badges170 silver badges303 bronze badges

Hỏi ngày 10 tháng 12 năm 2009 lúc 22:31Dec 10, 2009 at 22:31

Justin Staytonjustin StaytonJustin Stayton

5.7518 Huy hiệu vàng35 Huy hiệu bạc43 Huy hiệu đồng8 gold badges35 silver badges43 bronze badges

8

Sử dụng HTML2TEXT (ví dụ HTML vào văn bản), được cấp phép theo giấy phép công cộng Eclipse. Nó sử dụng các phương thức DOM của PHP để tải từ HTML và sau đó lặp lại DOM kết quả để trích xuất văn bản thuần túy. Cách sử dụng:

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

Mặc dù không đầy đủ, nó là nguồn mở và đóng góp được hoan nghênh.

Các vấn đề với các tập lệnh chuyển đổi khác:

  • Vì html2text (GPL) không tương thích với EPL.
  • Liên kết của Lkessler (Attribution) không tương thích với hầu hết các giấy phép nguồn mở.

Hướng dẫn php convert html to text - php chuyển đổi html thành văn bản

Abhi Beckert

32.1K12 Huy hiệu vàng82 Huy hiệu bạc110 Huy hiệu đồng12 gold badges82 silver badges110 bronze badges

Đã trả lời ngày 2 tháng 4 năm 2010 lúc 0:32Apr 2, 2010 at 0:32

8

Có chức năng dải_tags đáng tin cậy. Nó không đẹp mặc dù. Nó sẽ chỉ vệ sinh. Bạn có thể kết hợp nó với một chuỗi thay thế để có được sự nhấn mạnh của bạn.


", ""), array("_", "_"), $text));

// to preserve anchors...
str_replace("|a", "

Đã trả lời ngày 10 tháng 12 năm 2009 lúc 23:07Dec 10, 2009 at 23:07

pestilence669pestilence669pestilence669

5.6181 Huy hiệu vàng22 Huy hiệu bạc34 Huy hiệu đồng1 gold badge22 silver badges34 bronze badges

1

Chuyển đổi từ HTML sang văn bản bằng DomDocument là một giải pháp khả thi. Xem xét HTML2TEXT, yêu cầu PHP5:

  • http://www.howtocreate.co.uk/php/html2texthowto.html
  • http://www.howtocreate.co.uk/php/
  • http://www.howtocreate.co.uk/jslibs/termsOfUse.html

Về UTF-8, việc viết trên trang "Howto" trạng thái:

Hỗ trợ riêng của PHP cho Unicode khá kém và không phải lúc nào cũng xử lý UTF-8 một cách chính xác. Mặc dù tập lệnh HTML2TEXT sử dụng các phương thức an toàn Unicode-step (mà không cần mô-đun MBString), nhưng nó không thể luôn luôn đối phó với việc xử lý mã hóa của PHP. PHP không thực sự hiểu unicode hoặc mã hóa như UTF-8 và sử dụng mã hóa cơ sở của hệ thống, có xu hướng là một trong những gia đình ISO-8859. Kết quả là, những gì có thể nhìn vào bạn giống như một ký tự hợp lệ trong trình soạn thảo văn bản của bạn, trong UTF-8 hoặc một byte đơn, cũng có thể bị Php giải thích sai. Vì vậy, mặc dù bạn nghĩ rằng bạn đang nuôi một ký tự hợp lệ vào HTML2Text, bạn cũng có thể không.

Tác giả cung cấp một số cách tiếp cận để giải quyết vấn đề này và nói rằng phiên bản 2 của HTML2TEXT (sử dụng DomDocument) có hỗ trợ UTF-8.

Lưu ý các hạn chế cho sử dụng thương mại.

Hướng dẫn php convert html to text - php chuyển đổi html thành văn bản

Dave Jarvis

30K38 Huy hiệu vàng176 Huy hiệu bạc 307 Huy hiệu đồng38 gold badges176 silver badges307 bronze badges

Đã trả lời ngày 17 tháng 3 năm 2010 lúc 21:52Mar 17, 2010 at 21:52

lkesslerlkesslerlkessler

19.7K33 Huy hiệu vàng131 Huy hiệu bạc200 Huy hiệu Đồng33 gold badges131 silver badges200 bronze badges

1

Bạn có thể sử dụng Lynx với các tùy chọn -stdin và -dump để đạt được điều đó:

 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'


 TEST


Lorem Ipsum

"Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit..."

"There is no one who loves pain itself, who seeks after it and wants to have it, simply because it is pain..."

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.

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.

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"; }

Đã trả lời ngày 8 tháng 3 năm 2012 lúc 2:32Mar 8, 2012 at 2:32

nad2000nad2000nad2000

4.1481 Huy hiệu vàng27 Huy hiệu bạc23 Huy hiệu đồng1 gold badge27 silver badges23 bronze badges

Bạn có thể kiểm tra chức năng này

function html2text($Document) {
    $Rules = array ('@]*?>.*?@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);
}

Đã trả lời ngày 13 tháng 12 năm 2013 lúc 3:40Dec 13, 2013 at 3:40

HoangLong85HoangLong85HoangLong85

2874 Huy hiệu bạc2 Huy hiệu đồng4 silver badges2 bronze badges

1

Tôi không tìm thấy bất kỳ giải pháp nào phù hợp với các giải pháp hiện có - email HTML đơn giản đến các tệp văn bản đơn giản đơn giản.

Tôi đã mở kho lưu trữ này, hy vọng nó sẽ giúp được ai đó. Nhân tiện, giấy phép MIT :)

https://github.com/RobQuistNL/SimpleHtmlToText

Example:

$myHtml = 'This is HTML

Header



Newlines'; echo (new Parser())->parseString($myHtml);

returns:

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


Newlines

Đã trả lời ngày 21 tháng 11 năm 2016 lúc 15:34Nov 21, 2016 at 15:34

Hướng dẫn php convert html to text - php chuyển đổi html thành văn bản

RobrobRob

4.8694 Huy hiệu vàng25 Huy hiệu bạc40 Huy hiệu đồng4 gold badges25 silver badges40 bronze badges

3

public function plainText($text)
{
    $text = strip_tags($text, '

  • '); $text = preg_replace ('/<[^>]*>/', PHP_EOL, $text); return $text; }
  • 
    ", ""), array("_", "_"), $text));
    
    // to preserve anchors...
    str_replace("|a", "
    
    4

    
    ", ""), array("_", "_"), $text));
    
    // to preserve anchors...
    str_replace("|a", "
    
    5

    đầu ra chuỗi 1 chuỗi 2 chuỗi 3 chuỗi 4 chuỗi 5
    string 1
    string 2
    string 3
    string 4
    string 5

    Đã trả lời ngày 11 tháng 8 năm 2017 lúc 8:11Aug 11, 2017 at 8:11

    Aommy Indyaommy IndyAommy Indy

    Huy hiệu đồng 1191 Bạc5 Huy hiệu Đồng1 silver badge5 bronze badges

    1

    Nếu bạn muốn chuyển đổi các ký tự đặc biệt HTML và không chỉ loại bỏ chúng cũng như loại bỏ mọi thứ và chuẩn bị cho văn bản đơn giản thì đây là giải pháp hoạt động cho tôi ...

    function htmlToPlainText($str){
        $str = str_replace(' ', ' ', $str);
        $str = html_entity_decode($str, ENT_QUOTES | ENT_COMPAT , 'UTF-8');
        $str = html_entity_decode($str, ENT_HTML5, 'UTF-8');
        $str = html_entity_decode($str);
        $str = htmlspecialchars_decode($str);
        $str = strip_tags($str);
    
        return $str;
    }
    
    $string = '

    this is ( ) a test

    Yes this is! & does it get "processed"?
    ' htmlToPlainText($string); // "this is ( ) a test. Yes this is! & does it get processed?"`

    html_entity_decode w/ ent_quotes | ENT_XML1 chuyển đổi những thứ như

    
    ", ""), array("_", "_"), $text));
    
    // to preserve anchors...
    str_replace("|a", "
    
    6 htmlspecialchars_decode chuyển đổi những thứ như
    
    ", ""), array("_", "_"), $text));
    
    // to preserve anchors...
    str_replace("|a", "
    
    7 html_entity_decode chuyển đổi những thứ như
    
    ", ""), array("_", "_"), $text));
    
    // to preserve anchors...
    str_replace("|a", "
    
    8 và dải_tags xóa bất kỳ thẻ HTML nào còn lại.

    Đã trả lời ngày 15 tháng 5 năm 2018 lúc 14:36May 15, 2018 at 14:36

    Hướng dẫn php convert html to text - php chuyển đổi html thành văn bản

    JayjayJay

    4163 Huy hiệu bạc17 Huy hiệu đồng3 silver badges17 bronze badges

    Markdownify chuyển đổi HTML thành Markdown, một hệ thống định dạng văn bản đơn giản được sử dụng trên chính trang web này.

    Đã trả lời ngày 28 tháng 12 năm 2011 lúc 10:14Dec 28, 2011 at 10:14

    Outisoutisoutis

    73.5K20 Huy hiệu vàng147 Huy hiệu bạc216 Huy hiệu Đồng20 gold badges147 silver badges216 bronze badges

    1

    Tôi đã gặp phải vấn đề tương tự như OP và thử một số giải pháp từ các câu trả lời hàng đầu ở trên không chứng minh được hoạt động cho các kịch bản của tôi. Xem tại sao cuối cùng.

    Thay vào đó, tôi thấy tập lệnh hữu ích này, để tránh nhầm lẫn, hãy gọi nó là

    
    ", ""), array("_", "_"), $text));
    
    // to preserve anchors...
    str_replace("|a", "
    
    9, có sẵn theo GPL:

    • https://github.com/mtibben/html2text

    Đây thực sự là một phiên bản cập nhật của một tập lệnh đã được đề cập -

     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'
    
    
     TEST
    
    
    

    Lorem Ipsum

    "Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit..."

    "There is no one who loves pain itself, who seeks after it and wants to have it, simply because it is pain..."

    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.

    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.

    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"; }
    0 - được cập nhật bởi RoundCube Mail.

    Usage:

    $h2t = new \Html2Text\Html2Text('Hello, "world"');
    echo $h2t->getText(); // prints Hello, "WORLD"
    

    Tại sao

    
    ", ""), array("_", "_"), $text));
    
    // to preserve anchors...
    str_replace("|a", "
    
    9 tỏ ra tốt hơn những người khác:

    • Script

       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'
      
      
       TEST
      
      
      

      Lorem Ipsum

      "Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit..."

      "There is no one who loves pain itself, who seeks after it and wants to have it, simply because it is pain..."

      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.

      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.

      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"; }
      0 không hoạt động ngoài hộp cho các trường hợp có mã/tên HTML đặc biệt (ví dụ:
       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'
      
      
       TEST
      
      
      

      Lorem Ipsum

      "Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit..."

      "There is no one who loves pain itself, who seeks after it and wants to have it, simply because it is pain..."

      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.

      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.

      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"; }
      3) hoặc trích dẫn không ghép đôi (ví dụ:
       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'
      
      
       TEST
      
      
      

      Lorem Ipsum

      "Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit..."

      "There is no one who loves pain itself, who seeks after it and wants to have it, simply because it is pain..."

      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.

      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.

      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"; }
      4).

    • Script

       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'
      
      
       TEST
      
      
      

      Lorem Ipsum

      "Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit..."

      "There is no one who loves pain itself, who seeks after it and wants to have it, simply because it is pain..."

      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.

      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.

      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"; }
      5 không có tùy chọn để ẩn hoặc nhóm các liên kết ở cuối văn bản, làm cho một trang HTML thông thường trông đầy rẫy các liên kết khi ở định dạng toàn văn; Tùy chỉnh mã để xử lý đặc biệt về cách chuyển đổi được thực hiện không phải là thẳng như chỉ đơn giản là chỉnh sửa một mảng trong
      
      ", ""), array("_", "_"), $text));
      
      // to preserve anchors...
      str_replace("|a", "
      
      9.

    Đã trả lời ngày 24 tháng 11 năm 2016 lúc 16:10Nov 24, 2016 at 16:10

    Chris Devchris DevChris Dev

    3345 Huy hiệu bạc12 Huy hiệu Đồng5 silver badges12 bronze badges

    0

    Đối với các văn bản trong UTF-8, nó đã hoạt động cho tôi mb_convert_encoding. Để xử lý mọi thứ bất kể lỗi nào, hãy đảm bảo bạn sử dụng "@".

    Mã cơ bản tôi sử dụng là:

    $dom = new DOMDocument();
    @$dom->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));
    
    $body = $dom->getElementsByTagName('body')->item(0);
    echo $body->textContent;
    

    Nếu bạn muốn một cái gì đó nâng cao hơn, bạn có thể phân tích các nút, nhưng bạn sẽ gặp nhiều vấn đề với khoảng trắng.

    Tôi đã thực hiện một bộ chuyển đổi dựa trên những gì tôi nói ở đây. Nếu bạn quan tâm, bạn có thể tải xuống từ git https://github.com/kranemora/html2text

    Nó có thể phục vụ như một tài liệu tham khảo để làm cho của bạn

    Bạn có thể sử dụng nó như thế này:

    
    ", ""), array("_", "_"), $text));
    
    // to preserve anchors...
    str_replace("|a", "
    
    0

    Đã trả lời ngày 3 tháng 9 năm 2019 lúc 18:39Sep 3, 2019 at 18:39

    Hướng dẫn php convert html to text - php chuyển đổi html thành văn bản

    Tôi vừa tìm thấy một hàm PHP "dải_tags ()" và nó hoạt động trong trường hợp của tôi.

    Tôi đã cố gắng chuyển đổi HTML sau:

    
    ", ""), array("_", "_"), $text));
    
    // to preserve anchors...
    str_replace("|a", "
    
    1

    Sau khi áp dụng chức năng dải_tags (), tôi đã có đầu ra sau:

    
    ", ""), array("_", "_"), $text));
    
    // to preserve anchors...
    str_replace("|a", "
    
    2

    Đã trả lời ngày 16 tháng 5 năm 2012 lúc 21:17May 16, 2012 at 21:17

    Hướng dẫn php convert html to text - php chuyển đổi html thành văn bản

    Sudipsudipsudip

    2.6591 Huy hiệu vàng29 Huy hiệu bạc40 Huy hiệu đồng1 gold badge29 silver badges40 bronze badges

    1

    Nếu bạn không muốn tước hoàn toàn các thẻ và giữ nội dung bên trong các thẻ, bạn có thể sử dụng

     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'
    
    
     TEST
    
    
    

    Lorem Ipsum

    "Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit..."

    "There is no one who loves pain itself, who seeks after it and wants to have it, simply because it is pain..."

    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.

    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.

    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"; }
    7 và trích xuất
     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'
    
    
     TEST
    
    
    

    Lorem Ipsum

    "Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit..."

    "There is no one who loves pain itself, who seeks after it and wants to have it, simply because it is pain..."

    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.

    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.

    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"; }
    8 của nút gốc như thế này:

    
    ", ""), array("_", "_"), $text));
    
    // to preserve anchors...
    str_replace("|a", "
    
    3

    Một lợi thế của phương pháp này là nó không yêu cầu bất kỳ gói bên ngoài nào.

    Đã trả lời ngày 2 tháng 4 năm 2018 lúc 17:02Apr 2, 2018 at 17:02

    Hướng dẫn php convert html to text - php chuyển đổi html thành văn bản

    Supersansupersansupersan

    5.2452 Huy hiệu vàng39 Huy hiệu bạc61 Huy hiệu Đồng2 gold badges39 silver badges61 bronze badges

    Làm cách nào để chuyển đổi HTML thành văn bản bình thường?

    Chuyển đổi tệp HTML thành tệp văn bản (bảo tồn mã HTML và văn bản) ...
    Nhấp lại tab Tệp, sau đó nhấp vào tùy chọn Lưu dưới dạng ..
    Trong danh sách lưu xuống dưới dạng loại, chọn tùy chọn văn bản đơn giản (*. TXT).....
    Nhấp vào nút Lưu để lưu dưới dạng tài liệu văn bản ..

    Làm cách nào để viết văn bản HTML trong PHP?

    Hàm html_entity_decode () chuyển đổi các thực thể HTML thành các ký tự.Hàm html_entity_decode () trái ngược với htmlentity ().html_entity_decode() function converts HTML entities to characters. The html_entity_decode() function is the opposite of htmlentities().

    Làm cách nào để hiển thị thẻ HTML dưới dạng văn bản đơn giản trong PHP?

    If you want to use PHP to show HTML tags as text, then, you can use the function htmlspecialchars() to escape < and > characters.

    Chức năng PHP nào chuyển đổi HTML thành một định dạng có thể được hiển thị nhưng sẽ không được hiểu là HTML bởi một trình duyệt?

    Để chuyển đổi HTML thành một định dạng có thể được hiển thị nhưng sẽ không được hiểu là HTML bằng trình duyệt, hãy sử dụng chức năng PHP HTMLEntities.PHP htmlentities function.