javascript/php - 缩短字符串以适合特定的行数

发布于 2024-09-02 02:29:02 字数 339 浏览 2 评论 0 原文

我有一个字符串,必须适合一个盒子,并且长度最多为 3 行。为了缩短它,我计划截断它并以“...”结尾。我可以将其缩短为一定数量的字符,但如果我用“wwwwwwwww [...] wwww”使其看起来不错,那么用“iiiiiiiiiiii [...] iiii”看起来就不正确。

有没有什么方法可以缩短字符串占用的空间,而不是不使用固定宽度字体的字符串中的字符数?理想情况下,我想在服务器端(php)执行此操作,但认识到实际的字符宽度内容更有可能在客户端(JS / jQuery)

Mala

ps 中可行。请不要“简单地创建一个‘...’的图像并将其覆盖在行尾”黑客或类似的 - 我实际上想将字符串缩短到适当的长度

I have a string that must fit into a box, and must be at most 3 lines long. To shorten it, I plan to truncate it and end it with '...'. I could shorten it to a certain # of characters but if i make it look good with "wwwwwwwww [...] wwww" it won't look right with "iiiiiiiiiii [...] iiii".

Is there some way I can shorten it by how much space the string would take up, as opposed to how many characters there are in a string without using a fixed-width font? Ideally I'd like to do this server-side (php) but recognize that actual character width stuff is far more likely to be feasible client-side (JS / jQuery)

Mala

ps. Please no "simply create an image of '...' and overlay it over the end of the line" hacks or similar - I actually want to shorten the string to the appropriate length

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

何以畏孤独 2024-09-09 02:29:02

imageftbboximagettfbbox 为此。

There's imageftbbox and imagettfbbox for that.

攒眉千度 2024-09-09 02:29:02

此网站提供和任何方法一样好,并且是跨浏览器的。我喜欢简单:

This site offers as good a way as any, and is cross-browser. I like the simplicity:

你与清晨阳光 2024-09-09 02:29:02

我想不出有什么方法可以轻松地将字体大小设置为您所说的程度,但可能有一种方法可以“以您想要的方式显示”。我假设既然您提到了三行,我们正在讨论

<textarea rows="3" cols="50" style="overflow:hidden;"></textarea>

虽然文本在那里,但它不可见,并且滚动条不会显示。

There's no way that I can think of to easily get font sizes to the degree that you speak of, but there may be a hack that would "appear" the way you want. I assume since you mention three lines, we're talking about a <textarea> element:

<textarea rows="3" cols="50" style="overflow:hidden;"></textarea>

Although the text is there, it's not visible and the scroll bars would not display.

忆沫 2024-09-09 02:29:02

有许多 jQuery 插件可以实现此目的: http://plugins.jquery.com/taxonomy/期限/2229

There is a number of jQuery plugins for this: http://plugins.jquery.com/taxonomy/term/2229

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文