How to strip HTML tags, CSS from a string?

别等时光非礼了梦想. 提交于 2019-12-22 07:01:38

问题


I have string such as

<p>
    <style type="text/css">
P { margin-bottom: 0.21cm; direction: ltr; color: rgb(0, 0, 0); }P.western { font-family: "Times New Roman",serif; font-size: 12pt; }P.cjk { font-family: "Arial Unicode MS",sans-serif; font-size: 12pt; }P.ctl { font-family: "Tahoma"; font-size: 12pt; }    </style>
</p>
<p align="CENTER" class="western" style="margin-bottom: 0cm">
    <font size="5" style="font-size: 20pt"><u><b> TEXT I WANT TO GET </b></u></font></p>

How can i strip html, css and get only text?

Im aware of strip_tags(), and I can write function with preg_replace, but is there a working solution for php? Thanks.


回答1:


Use:

<?php

$text = '<p>
    <style type="text/css">
P { margin-bottom: 0.21cm; direction: ltr; color: rgb(0, 0, 0); }P.western { font-family: "Times New Roman",serif; font-size: 12pt; }P.cjk { font-family: "Arial Unicode MS",sans-serif; font-size: 12pt; }P.ctl { font-family: "Tahoma"; font-size: 12pt; }    </style>
</p>
<p align="CENTER" class="western" style="margin-bottom: 0cm">
    <font size="5" style="font-size: 20pt"><u><b> TEXT I WANT TO GET </b></u></font></p>';

$text = strip_tags($text,"<style>");

$substring = substr($text,strpos($text,"<style"),strpos($text,"</style>")+2);

$text = str_replace($substring,"",$text);
$text = str_replace(array("\t","\r","\n"),"",$text);
$text = trim($text);

echo $text;

?>



回答2:


This worked for me .

function strip_tag_css($text){


    $text = strip_tags($text,"<style>");

    $substring = substr($text,strpos($text,"<style"),strpos($text,"</style>")+2);

    $text = str_replace($substring,"",$text);
    $text = str_replace(array("\t","\r","\n"),"",$text);
    $text = trim($text);

    return $text;
}


$bodymensage = str_replace('  ','',html_entity_decode( strip_tag_css(strip_tags($message)), ENT_QUOTES, "utf8" ));



回答3:


To make sure that occurrences of style tag are removed and all whitespace reduced to a single space:

$text = '<p>
<style type="text/css">
P { margin-bottom: 0.21cm; direction: ltr; color: rgb(0, 0, 0); }P.western { font- 
family: "Times New Roman",serif; font-size: 12pt; }P.cjk { font-family: "Arial Unicode 
MS",sans-serif; font-size: 12pt; }P.ctl { font-family: "Tahoma"; font-size: 12pt; }    
</style>
</p>
<p align="CENTER" class="western" style="margin-bottom: 0cm">
    <font size="5" style="font-size: 20pt"><u><b> TEXT I WANT TO GET </b></u></font> 
</p>
<style>a { color: red}</style>';

$text = strip_tags($text, '<style>');
$start = strpos($text, '<style');
// All of occurrences of <style>.
while ($start !== false) {
    $end = strpos($text, '</style>');
    if (!$text) {
        break;
    }
    $diff = $end - $start + strlen('</style>');
    $substring = substr($text, $start, $diff);
    $text = str_replace($substring, '', $text);
    $start = strpos($text, '<style');
}

// Remaining <style> if any.
$text = strip_tags($text);

// Remove all new lines and tabs and use a space instead.
$text = str_replace(["\n", "\r", "\t"], ' ', $text);

// Trim left and right.
$text = trim($text);

// Remove all spaces that have more than one occurrence.
$text = preg_replace('!\s+!', ' ', $text);

echo $text;

Here's the output.



来源:https://stackoverflow.com/questions/18587830/how-to-strip-html-tags-css-from-a-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!