dongtao9158 2010-11-20 10:42
浏览 23

在PHP中,我应该如何缩短标题和段落中的长字符串以使它们适合固定边界?

When building websites I'm forever chopping up strings to make them display nicely as headings and paragraphs. I use the substr function to chop-off unwanted characters and then add in ellipses. For example:

if ( strlen ( $mystring ) > 22 ) {
 echo substr( $mystring,0,21 ).'...';
} else {
 echo $mystring;
}

This works pretty good most of the time, but it is far from perfect. Check out how the shortened headings look on one of my sites. You can clearly see a lot of inconsistency in how the shortened headings look.

Surely, there is a better PHP method/ technique?

  • 写回答

4条回答 默认 最新

  • duanchu0031 2010-11-20 10:45
    关注

    Edit: My bad, you want ellipsis... Your ellipses looks fine on that page you showed...

    If you Really want them to line up you could put the text in an inline element with max width such and such and overflow: hidden followed by a seperate element with the ellipsis.

    评论

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题