如何从字符串中去除 HTML 标签、CSS? [英] How to strip HTML tags, CSS from a string?

查看:42
本文介绍了如何从字符串中去除 HTML 标签、CSS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的字符串

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

如何去除 html、css 并只获取文本?

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

我知道 strip_tags(),我可以用 preg_replace 编写函数,但是 php 有没有可行的解决方案?谢谢.

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

推荐答案

使用:

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

?>

这篇关于如何从字符串中去除 HTML 标签、CSS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆