gpt4 book ai didi

php - 如何从字符串中剥离 HTML 标签、CSS?

转载 作者:可可西里 更新时间:2023-11-01 13:45:19 26 4
gpt4 key购买 nike

我有这样的字符串

<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 的解决方案?谢谢。

最佳答案

使用:

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

?>

关于php - 如何从字符串中剥离 HTML 标签、CSS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18587830/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com