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

Cha*_*est 3 html php

编辑:关键点是获取前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>
Run Code Online (Sandbox Code Playgroud)

谢谢.

dye*_*awn 6

<?php 
$your_string_without_tags = strip_tags($your_string); 
$your_200_char_string = substr($your_string_without_tags, 0, 200); 
?>
Run Code Online (Sandbox Code Playgroud)

关闭字符串中的打开HTML标记

  • http://stackoverflow.com/questions/3810230/php-how-to-close-open-html-tag-in-a-string (2认同)