由于长度,截断的字符串具有特殊字符

时间:2013-12-20 21:21:22

标签: php string special-characters

我需要限制字符串的长度。我是这样做的:

$str.='<div class="timt">'.substr($row['summary'],0,180).'</div>';

问题在于,在我的语言中,有一些特殊的字符,如áéíúó,当字符串“以特殊字符结束”时,行的结尾为h&eac而不是。<登记/> 我认为这是因为&eacute或类似的东西...但我怎么能解决这个问题,所以这不是“截断”?

我找到了一些建议,但没有一个是有效的How to truncate HTML with special characters? ..有没有人有机会指出我怎么能解决这个问题。

我也试过了,但它没有用。

$str.='<div class="timt">'.mb_strcut($row['summary'],0,180,"UTF-8").'</div>';

安德烈回答后,我试试这个

$description = html_entity_decode($row['summary']);
$str.='<div class="timedesctt" onclick="location.href=\''.$this->make_url("item/view/".$row['itemid'],array('city_name'=>$this->get_english_city_name($row['city']))).'\';">'.mb_strcut($description,0,180,"UTF-8").'</div>';

现在只有问题..它显示?而不是á

我的数据库编码为utf8 unicode ci,charset设置为utf8

解决方案:html_entity_decode($strint,ENT_QUOTES, 'UTF-8')

1 个答案:

答案 0 :(得分:2)

最简单的方法是使用html_entity_decode()转换html实体。 然后使用mb_substr(),然后将其转换回来。

我可以提供必要的示例,但请检查html_entity_decode()

相关问题