如何从字符串中去除 HTML 标签、CSS?

2024-03-16

我有字符串如

<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 并只获取文本?

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


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;

?>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何从字符串中去除 HTML 标签、CSS? 的相关文章

随机推荐