PHP Substr 函数修整问题 [英] PHP Substr Function Trimming Problem

查看:51
本文介绍了PHP Substr 函数修整问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用下面的代码来修剪标题并在我的 最近的帖子部分href="http://www.sivrisinema.com" rel="nofollow noreferrer">博客:

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) . '&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 字符实体.

But there are problem with HTML character entities. When i use substr function for trim a title, substr function trimming HTML character entities too.

所以我尝试使用 html_entity_decode 功能,但我做的不是很好.

So i tried to use html_entity_decode function but i can't do it very well.

有人可以帮我吗?

推荐答案

试试这个:

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

这将解码所有实体,因此 substr 不会破坏任何内容.之后,您可以将所有字符编码回其实体.

This will decode all entities so substr won't break anything. After that you can encode all characters back to their entities.

这篇关于PHP Substr 函数修整问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆