【问题标题】:Convert HTML output into a plain text using php使用 php 将 HTML 输出转换为纯文本
【发布时间】:2011-12-06 16:03:26
【问题描述】:

我正在尝试将我的示例 HTML 输出转换为纯文本,但我不知道如何操作。我使用 file_get_contents 但我尝试转换的页面返回最相似。

$raw = "http://localhost/guestbook/profiles.php";
$file_converted = file_get_contents($raw);
echo $file_converted;

profiles.php

<html>
    <head>
        <title>Profiles - GuestBook</title>
        <link rel="stylesheet" type="text/css" href="css/style.css">
    </head>
<body>
    <!-- Some Divs -->
    <div id="profile-wrapper">
        <h2>Profile</h2>
        <table>
            <tr>
                <td>Name:</td><td> John Dela Cruz</td>
            </tr>
            <tr>
                <td>Age:</td><td>15</td>
            </tr>
            <tr>
                <td>Location:</td><td> SomewhereIn, Asia</td>
            </tr>
        </table>
    </div>
</body>
</html>

基本上,我试图回显这样的内容(纯文本,无样式)

Profile
Name: John Dela Cruz
Age: 15
Location: SomewhereIn, Asia

但我不知道怎么做。 :-( 。请帮帮我,提前谢谢你们。

编辑:由于我只是在页面内容之后,无论它是样式还是纯文本,有没有办法使用 file_get_contents() 只选择(见下面的代码)?

 <h2>Profile</h2>
        <table>
            <tr>
                <td>Name:</td><td> John Dela Cruz</td>
            </tr>
            <tr>
                <td>Age:</td><td>15</td>
            </tr>
            <tr>
                <td>Location:</td><td> SomewhereIn, Asia</td>
            </tr>
        </table>

【问题讨论】:

  • 感谢 Sergej Jevsejev、josnidhin 和 Jonathan Rich 的快速响应。倍受赞赏。 :-)
  • 请注意,说要使用strip_tags 的人并不完全理解或粗心。它将保留您的标题以及任何内联样式表或 JavaScript。你没有后者,但你有一个头衔。 . .
  • 我使用了 strip_tags,虽然它会去掉 html 标签,但它会返回一个不受欢迎的输出,例如{ 字体:粗体 11px Lucida Grande、Lucida Sans Unicode、Trebuchet MS、Helvetica、Arial、sans-serif;颜色:#045877;填充:15px 0 0 12px;文字装饰:无;显示:块;边距:0 自动; }
  • 也许使用 strip_tags 肯定会回答我的问题的标题,正如我所见,它是纯文本,但使用 strip_tags 并不能帮助我返回理想的输出。
  • 使用 strip_tags 或者如果您需要带有 html 标签的纯文本,那么:browse-tutorials.com/snippet/…

标签: php html plaintext


【解决方案1】:

使用phpstrip_tags

如果 strip_tags 不起作用,那么也许你可以使用正则表达式来提取你想要的信息。

尝试使用 PHP preg_match/(&lt;td&gt;.*?&lt;\/td&gt;)/ 作为模式

【讨论】:

  • 没错,如果您不需要空格或其他符号(字符),请参阅trim
  • 这不是exactly。它将以纯文本形式保留他的标题。
  • 我使用了 strip_tags,虽然它会去掉 html 标签,但它会返回一个不受欢迎的输出,例如{ 字体:粗体 11px Lucida Grande、Lucida Sans Unicode、Trebuchet MS、Helvetica、Arial、sans-serif;颜色:#045877;填充:15px 0 0 12px;文字装饰:无;显示:块;边距:0 自动; }
【解决方案2】:

看看 simplexml_load_file():

http://www.php.net/manual/en/function.simplexml-load-file.php

它将允许您将 HTML 数据加载到对象 (SimpleXMLElement) 中并像树一样遍历该对象。

【讨论】:

  • 如果你为他添加一个例子,这将是最好的答案。现在对初学者来说太模糊了。
  • 对不起,不知道用xml加载文件
  • us.php.net/manual/en/simplexml.examples-basic.php 基本用法很简单。你有使用 PHP 对象接口的经验吗?
  • @Jonathan Rich:不,我只是 Php 的新手,对不起先生
  • 没关系。看看我粘贴的例子,看看你能想出什么。
【解决方案3】:

尝试使用 PHP 函数 strip_tags

【讨论】:

  • 我使用了 strip_tags,虽然它会去掉 html 标签,但它会返回一个不受欢迎的输出,例如{ 字体:粗体 11px Lucida Grande、Lucida Sans Unicode、Trebuchet MS、Helvetica、Arial、sans-serif;颜色:#045877;填充:15px 0 0 12px;文字装饰:无;显示:块;边距:0 自动; }
【解决方案4】:

试试这个,

<?php
$data = file_get_contents("your_file");
preg_match_all('|<div[^>]*?>(.*?)</div>|si',$data, $result);
print_r($result[0][0]);
?>

我已经尝试过这个,它似乎对我有用,我希望对你也有用

【讨论】:

  • 您好先生,在数据变量上放什么?
  • 只需将http://localhost/guestbook/profiles.php 添加到您的变量中,然后对于print_r(),您可以使用foreach() 或类似的东西
【解决方案5】:

您可以为此使用strip_tags php 函数。浏览 strip_tags 函数的 php 手册页中的 cmets,看看如何以一种好的方式使用它。

【讨论】:

    猜你喜欢
    • 2010-10-20
    • 1970-01-01
    • 2011-01-25
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-12
    • 2014-09-08
    相关资源
    最近更新 更多