gpt4 book ai didi

php - 获取字符串的前 200 个字符而不破坏末尾的 HTML 标记

转载 作者:可可西里 更新时间:2023-11-01 01:06:53 24 4
gpt4 key购买 nike

编辑:关键点是获取前 200 个字符并关闭所有打开的标签。


我目前正在通过 DOM 对象从其他网站加载文章(其他网站没有 RSS)。我想对该文章进行“预览”,但这是我的问题:

  1. 我无法控制文章的编写方式,因此它似乎总是在第二个 TR 的表格中(他们使用 CMS,而且很乱 - 请参见下面的示例)。

    <
  2. 它们有很多 HTML 标签,我不想让一个打开。

  3. 我需要保留 HTML 格式,我知道它很难看,但它非常适合我的页面。

不是我的代码(法语抱歉):

<table>

<TR >

<TD class='Normal' valign="top" colspan="2" style="padding-bottom:15px;">13-01-2012 <b>Water-polo – Championnat pan-pacifique<b ></TD>

</TR><TR >

<TD class='Normal' valign="top"><span class="HeadTitleNews"> Les Canadiennes disputeront le bronze aux Chinoises</span> <img src='http://www.sportcom.qc.ca/Portals/0/2011WaterpoloF.jpg' width='165' align='right' class='imgAnnouncementCss'><div style="margin: 0in 0in 0pt"><span style="font-family: Tahoma; font-size: 10pt">Montr&eacute;al, 13 janvier 2012 (Sportcom) &ndash; L&rsquo;&eacute;quipe f&eacute;minine canadienne de water-polo a remport&eacute; une victoire &eacute;crasante de 19-3 face au Br&eacute;sil, vendredi, au Championnat pan-pacifique de Melbourne, en Australie. Les Canadiennes se sont ainsi assur&eacute;es de participer au match pour la m&eacute;daille de bronze contre les Chinoises. </span></div>

<div style="margin: 0in 0in 0pt">&nbsp;</div>

<div style="margin: 0in 0in 0pt"><span style="font-family: Tahoma; font-size: 10pt">La Montr&eacute;alaise <strong>Sophie</strong></span><strong><span style="font-family: Tahoma; font-size: 10pt"> Baron-La Salle</span></strong><span style="font-family: Tahoma; font-size: 10pt"> a marqu&eacute; quatre buts dans la victoire. </span></div>

<div style="margin: 0in 0in 0pt">&nbsp;</div>

谢谢。

最佳答案

<?php 
$your_string_without_tags = strip_tags($your_string);
$your_200_char_string = substr($your_string_without_tags, 0, 200);
?>

Close open HTML tags in a string

关于php - 获取字符串的前 200 个字符而不破坏末尾的 HTML 标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8856794/

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