通过php将HTML转换为TXT文件

通过php将HTML转换为TXT文件,php,Php,我需要将HTML转换为txt输出。像下面的例子一样,通过php,可以吗 例: HTML格式: <table> <tr> <td>Srinivasan-1</td> <td>welcome-1</td> </tr> <tr> <td>Srinivasan-2</td> <td>

我需要将HTML转换为txt输出。像下面的例子一样,通过php,可以吗

例:

HTML格式:

<table>
    <tr>
        <td>Srinivasan-1</td>
        <td>welcome-1</td>
    </tr>
    <tr>
         <td>Srinivasan-2</td>
         <td>welcome-2</td>
    </tr>
</table>

嗯。。。PHP strip_标记函数是否因某些原因而不充分

您可以使用str_replace分别用一个制表符和一个CRLF替换每个关闭的td和tr,以获得您似乎正在寻找的输出布局。

您能试试吗

  <?php
    $ones= "<table><tr><td>Srinivasan-1</td><td>welcome-1</td></tr><tr><td>Srinivasan-2</td><td>welcome-2</td></tr></table>";

    $ones= strip_tags($ones);

    $file = fopen("test.txt","w");

    fwrite($file,$ones);

    fclose($file);
    ?>


不管怎样,它都不会像你所希望的那样输出。但我希望这是写入文件的基本方法。

如果您希望文本保持表格中的格式,那么解决方案将相当复杂。使用内置的PHP函数,您将无法保持列之间的均匀间距,因为它不再是一个表,而只是一个文本

你能做的就是使用这个函数去掉HTML。可能需要在列之间放置制表符,使其看起来仍然是一个表。这是我突然想到的一个函数:

<?php
    function StripHtmlFromTable($html_in)
    {
        $html_in = str_replace("</td>", "\t", $html_in); // Space the columns.
        $html_in = str_replace("</tr>", "\n", $html_in); // Put each row on a new line.
        $text_out = strip_tags($html_in);

        return $text_out;
    }
?>

我在计算机上测试了脚本,下面是输出的图像:


我必须将输出放入
标记中,这样您就可以看到函数如何使用空白来格式化文本。输出与示例输出完全相同,我希望这就是您要查找的内容。

您可以使用一组正则表达式,并将其替换为换行符和制表符:

<?php
$raw = '<table>
    <tr>
        <td>Srinivasan-1</td>
        <td>welcome-1</td>
    </tr>
    <tr>
         <td>Srinivasan-2</td>
         <td>welcome-2</td>
    </tr>
</table>';
    $patterns = array(
        '/[\n\t]/si', /* remove existing whitespace and linebreaks */
        '/<tr.*?>/s', /* beginning of a row (new line) */
        '/<td.*?>([^<]+)<\/td>/s' /* all cells*/
    );
    $replaces = array(
        "", /* remove whitespace */
        "\n", /* add a new line for each tr */
        "\t$1" /* add a indent and the content of each cell */
    );
    // run the preg replace and strip all other tags
    $text = strip_tags(preg_replace($patterns,$replaces,$raw));
    echo $text;
?>


是的,你可以使用
strip\u tags()
函数嘿,伙计,我试过strip\u tags(),但它会删除所有标记,我还需要html对齐。。示例:“tr”“td”对齐方式that@Srinivasan这不是你想要的吗?您说过要将HTML转换为文本。如果您希望文本具有HTML对齐方式,那么不要从中删除HTML,就这么简单。另一方面,我发布了一个答案,它删除了所有的HTML,但它也使用空格来保持对齐,试试看。嗨,伙计,我已经检查了你的建议,我需要这种类型的函数,但是我如何对齐像@Srinivasan这样的单独“td”我想不出任何方法,这将是一个非常复杂的任务,它可能需要正则表达式。如果希望文本的行为类似于表格中的文本,请将其保留在表格中。因为,由于列是由实际的空格分隔的,因此需要根据单元格内容的长度计算出要在何处放置多少空格。为什么你需要从中去掉HTML?Miller,因为我需要用点阵打印机打印这个表单,所以我要尝试这个任务哦。嗯,我甚至不知道从哪里开始右侧对齐,对不起。
<?php
$raw = '<table>
    <tr>
        <td>Srinivasan-1</td>
        <td>welcome-1</td>
    </tr>
    <tr>
         <td>Srinivasan-2</td>
         <td>welcome-2</td>
    </tr>
</table>';
    $patterns = array(
        '/[\n\t]/si', /* remove existing whitespace and linebreaks */
        '/<tr.*?>/s', /* beginning of a row (new line) */
        '/<td.*?>([^<]+)<\/td>/s' /* all cells*/
    );
    $replaces = array(
        "", /* remove whitespace */
        "\n", /* add a new line for each tr */
        "\t$1" /* add a indent and the content of each cell */
    );
    // run the preg replace and strip all other tags
    $text = strip_tags(preg_replace($patterns,$replaces,$raw));
    echo $text;
?>