【问题标题】:Create plain text from HTML [duplicate]从 HTML 创建纯文本 [重复]
【发布时间】:2014-05-14 18:34:06
【问题描述】:

我正在开发一个使用 php 将 HTML 转换为纯文本版本的函数。我尝试使用strip_tags() 如下,

  $html='<style type="text/css">
  @media only screen and (max-width: 480px) {
    .message_mobile {
        width: 100% !important;
    }
  }
 </style>
<p class="message_mobile"> sample Text</p>';
$plain_text       =strip_tags($html);
echo $plain_text;

但它会产生类似的输出,

 @media only screen and (max-width: 480px) {
    .message_mobile {
        width: 100% !important;
    }
  }

  sample Text

但我不需要&lt;style&gt; 标签内的内容。如何做到这一点? 我还有一个问题,当我尝试用桌子剥离标签时,它会产生不需要的线刹车。如何解决这些问题? 有没有什么好的方法可以从 HTML 创建纯文本?

【问题讨论】:

标签: php html strip-tags


【解决方案1】:

使用此功能:

<?php

function strip_html_tags($str){
    $str = preg_replace('/(<|>)\1{2}/is', '', $str);
    $str = preg_replace(
        array(// Remove invisible content
            '@<head[^>]*?>.*?</head>@siu',
            '@<style[^>]*?>.*?</style>@siu',
            '@<script[^>]*?.*?</script>@siu',
            '@<noscript[^>]*?.*?</noscript>@siu',
            ),
        "", //replace above with nothing
        $str );
    $str = replaceWhitespace($str);
    $str = strip_tags($str);
    return $str;
} //function strip_html_tags ENDS

//To replace all types of whitespace with a single space
function replaceWhitespace($str) {
    $result = $str;
    foreach (array(
    "  ", " \t",  " \r",  " \n",
    "\t\t", "\t ", "\t\r", "\t\n",
    "\r\r", "\r ", "\r\t", "\r\n",
    "\n\n", "\n ", "\n\t", "\n\r",
    ) as $replacement) {
    $result = str_replace($replacement, $replacement[0], $result);
    }
    return $str !== $result ? replaceWhitespace($result) : $result;
}


$html='<style type="text/css">
  @media only screen and (max-width: 480px) {
    .message_mobile {
        width: 100% !important;
    }
  }
 </style>
<p class="message_mobile"> sample Text</p>';
$plain_text = strip_html_tags($html);
echo $plain_text;

【讨论】:

    【解决方案2】:

    您要查找的函数是htmlspecialchars

    这段代码:

    <?php
        $htmltag  = '
        <style type="text/css">
            @media only screen and (max-width: 480px) {
                .message_mobile {
                    width: 100% !important;
                }
            }
        </style>
        <p class="message_mobile"> sample Text</p>';
        echo "<pre>".nl2br(htmlspecialchars($htmltag))."</pre>";
    ?>
    

    将在您的网站上创建此输出:

    <style type="text/css">
    
        @media only screen and (max-width: 480px) {
    
            .message_mobile {
    
                width: 100% !important;
    
            }
    
        }
    
    </style>
    
    <p class="message_mobile"> sample Text</p>
    

    【讨论】:

      【解决方案3】:

      您可以使用类从 HTML 创建纯文本。

      访问此链接可能会对您有所帮助。 Converting HTML to plain text in PHP for e-mail

      类:http://www.howtocreate.co.uk/php/html2texthowto.html

      试试这个,对我有帮助

      http://code.google.com/p/iaml/source/browse/trunk/org.openiaml.model.runtime/src/include/html2text

      【讨论】:

        猜你喜欢
        • 2010-10-08
        • 1970-01-01
        • 1970-01-01
        • 2017-08-28
        • 2020-03-25
        • 2013-02-21
        • 2012-05-22
        • 1970-01-01
        相关资源
        最近更新 更多