1

我正在使用下面的代码来修剪标题并在我的博客上显示最近的帖子部分

<?php global $post;
$releaseDate = get_post_meta($post->ID, "gosterim_tarihi", true);
foreach( $images as $image ) {
    $title = get_the_title();
    if (strlen($title) > 20) { $title = substr($title, 0, 20) . '&hellip;'; }
    $attachmentimage=wp_get_attachment_image_src( $image->ID, 'large' );
    echo '<li><a href="'. get_permalink() .'" title="' . $title . '"><img src="'. $attachmentimage[0] .'" alt="'. $title .'" />'. $title .'<span>Gösterim Tarihi: ' . $releaseDate . '</span></a></li>';
} ?>

但是 HTML 字符实体存在问题。当我使用substr修剪标题的substr功能时,修剪HTML字符实体的功能也是如此。

所以我尝试使用html_entity_decode功能,但我不能很好地做到这一点。

任何人都可以帮助我吗?

4

4 回答 4

3

尝试这个:

$output = htmlentities(substr(html_entity_decode($input), 0, 20));

这将解码所有实体,因此substr不会破坏任何东西。之后,您可以将所有字符编码回它们的实体。

于 2010-01-23T18:54:43.083 回答
0

我认为您可以在那里使用strip_tags功能,例如:

substr(strip_tags($title), 0, 20);

这将只处理不包括任何 html 字符的标题。

于 2010-01-23T18:41:51.430 回答
0

使用此功能

<?php
  function keephtml($string){
          $res = htmlentities($string);
          $res = str_replace("&lt;","<",$res);
          $res = str_replace("&gt;",">",$res);
          $res = str_replace("&quot;",'"',$res);
          $res = str_replace("&amp;",'&',$res);
          return $res;
}
?>
于 2010-01-23T19:02:45.200 回答
0

如果您可以将 html 编码保留到最后一分钟,例如当您实际回显 $title 字符串时,它会更清晰。当然,这意味着您必须记住自己对所有字符串进行编码。

于 2010-01-23T19:03:39.203 回答