Joe*_*oeW 7 php truncate character-encoding substr ucfirst
我们的网站是中文的,主页面的一部分显示了其他页面标题的列表,其最大长度被称为'26'(我假设这是使用英文字符计数,如果中文字符是使用英语?).我们用于此的线是:
<?php echo anchor('projects/'.$rs->url_project_title.'/'.$rs->project_id,substr(ucfirst($rs->project_title),0,26),'style="text-decoration:none;"'); ?>
但是,如果标题确实很长,那么代码会截断它,但是最后两个中文字符总是显示为 ,因为我猜它是使用英文版本的单词并分割中文字符(不知何故).也许我在想这个!?
例如....
原版的:
?????????????????????
截断版本:
??????????
您是否可以建议进行修改以启用所需数量的字符而不会产生 ??
而不是substr使用mbstring功能:
echo anchor(
'projects/' . $rs->url_project_title . '/' . $rs->project_id,
mb_substr(ucfirst($rs->project_title), 0, 26),
'style="text-decoration:none;"'
);
Run Code Online (Sandbox Code Playgroud)
如果你没有成功,那么PHP可能没有检测到字符串编码,因此请提供正确的编码mb_substr():
// PHP uses internal encoding mb_internal_encoding()
echo mb_substr($string, 0, 26);
// you specify the encoding - in the case you know in which encoding the input comes
echo mb_substr($string, 0, 26, 'UTF-8');
// PHP tries to detect the encoding
echo mb_substr($string, 0, 26, mb_detect_encoding($string));
Run Code Online (Sandbox Code Playgroud)
另请参阅mb_detect_encoding()更多信息.
希望这可以帮助.