获取字符串的前200个字符,而不会在最后破坏HTML标记

时间:2012-01-13 20:26:28

标签: php html

编辑:关键点是获取前200个字符并关闭所有标记,这些标记保持打开状态。


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

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

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

  3. 我需要保留HTML格式,我知道它很丑,但它完全适合我的页面。

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

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

    感谢。

2 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:-1)

您可以使用strip_tags功能删除所有html标记,然后根据需要使用它。

相关问题