Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从HTML创建纯文本_Php_Html_Strip Tags - Fatal编程技术网

Php 从HTML创建纯文本

Php 从HTML创建纯文本,php,html,strip-tags,Php,Html,Strip Tags,我正在开发一个函数,该函数将使用php将HTML转换为纯文本版本 $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&g

我正在开发一个函数,该函数将使用php将HTML转换为纯文本版本

  $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;
但是我不需要
标签中的内容。如何做到这一点? 我还有另一个问题,当我试图用一张表剥离标签时,它会产生不需要的线路制动器。如何解决这些问题? 有没有从HTML创建纯文本的好方法?

使用此功能:

<?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;

您要查找的函数是

此代码:

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

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


@仅介质屏幕和(最大宽度:480px){
.message_mobile{
宽度:100%!重要;
}
}

示例文本


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

请访问此链接,这可能会对您有所帮助。

课程:

试试这个,它对我有帮助


请检查此项one@Jenz在html2text的帮助下,我的问题解决了。谢谢
<?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>