微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php-获取字符串的前200个字符,但不要在末尾破坏HTML标记

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

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

>我不控制文章的编写方式,因此,它似乎总是位于第二个TR的表中(它们使用CMS,而且很乱-请参见下面的示例).
>他们有很多HTML标记,我不想公开.
>我需要保留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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐