PHP Substr 函数修剪问题
我使用下面的代码来修剪标题,并将其显示在我的 最近帖子部分 http://www.sivrisinema.com" rel="nofollow noreferrer">博客:
<?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) . '…'; }
$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
功能,但我不能做得很好。
有人可以帮助我吗?
I'm using below code for triming titles and show it recent posts section on my blog:
<?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) . '…'; }
$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>';
} ?>
But there are problem with HTML character entities. When i use substr
function for trim a title, substr
function trimming HTML character entities too.
So i tried to use html_entity_decode
function but i can't do it very well.
Anyone can help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
试试这个:
这将解码所有实体,因此
substr
不会破坏任何内容。之后,您可以将所有字符编码回其实体。Try this:
This will decode all entities so
substr
won't break anything. After that you can encode all characters back to their entities.我认为你可以使用 strip_tags 函数,例如:
这会处理仅包含标题,不包括任何 html 字符。
I think you can use the strip_tags function there eg:
This will deal only with the title excluding any html chars.
使用此功能
Use This Function
如果您可以将 html 编码留到最后一刻(例如,当您实际回显 $title 字符串时),则会更清晰。当然,这意味着您必须记住自己对所有字符串进行编码。
It would be cleaner if you can leave the html-encoding until the last minute, e.g. when you actually echo the $title string. Of course, that means you have to remember to encode all the strings yourself.