缩短句子的算法存在问题
我有一个网页,显示多个文本条目,其长度没有限制。如果它们太长,它们会被自动剪切以避免换行。这是用于剪切它们的 PHP 函数:
function cutSentence($sentence, $maxlen = 16) {
$result = trim(substr($sentence, 0, $maxlen));
$resultarr = array(
'result' => $result,
'islong' => (strlen($sentence) > $maxlen) ? true : false
);
return $resultarr;
}
如下图所示,结果很好,但有一些例外。包含多个 Ms (我必须考虑到这些)的字符串将转到换行符。
现在,所有字符串在仅 16 个字符后都会被剪切,这已经非常低并且难以阅读。
我想知道是否存在一种方法来确保需要更多空格的句子得到它,并且那些包含宽字符的句子最终会被剪切为更少的字符数(请不要建议使用 CSS 属性 text-overflow: ellipsis因为它没有得到广泛的支持,并且它不允许我使“...”可点击链接到完整的条目,并且我不惜一切代价需要它)。
提前致谢。
I have a webpage which displays multiple textual entries which have no restriction on their length. They get automatically cut if they are too long to avoid going to a new line. This is the PHP function to cut them:
function cutSentence($sentence, $maxlen = 16) {
$result = trim(substr($sentence, 0, $maxlen));
$resultarr = array(
'result' => $result,
'islong' => (strlen($sentence) > $maxlen) ? true : false
);
return $resultarr;
}
As you can see in the image below, the result is fine, but there are a few exceptions. A string containing multiple Ms (I have to account for those) will go to a newline.
Right now all strings get cut after just 16 characters, which is already very low and makes them hard to read.
I'd like to know if a way exists to make sure sentences which deserve more spaces get it and those which contain wide characters end up being cut at a lower number of characters (please do not suggest using the CSS property text-overflow: ellipsis because it's not widely supported and it won't allow me to make the "..." click-able to link to the complete entry, and I need this at all costs).
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用固定宽度的字体,这样所有字符的宽度都相等。或者可选地获取每个字符的宽度有多少像素,然后将它们加在一起,并删除像素长度超过一定量的附加字符。
You could use a fixed width font so all characters are equal in width. Or optionally get how many pixels wide every character is and add them together and remove the additional character wont the pixel length is over a certain amount.
如果应用程序的风格不太重要,您可以简单地使用等宽字体系列中的字体,例如 Courier。
If the style of your application isn't too important, you could simply use a font in the monospace family such as Courier.
使用 Javascript 而不是 PHP 来完成。使用 DOM 属性
offsetWidth
获取包含元素的宽度。如果它超过某个最大宽度,则相应地截断。代码复制自 如何在 Firefox 中模拟 text-overflow: ellipsis ?:
Do it in Javascript rather than in PHP. Use the DOM property
offsetWidth
to get the width of the containing element. If it exceeds some maximum width, then truncate accordingly.Code copied from How can I mimic text-overflow: ellipsis in Firefox? :
既然您要求一个网页,那么您可以使用 CSS text-overflow 来做到这一点。
它似乎得到了足够的支持,对于 Firefox 来说似乎有 css 解决方法 或 jquery 解决方法...
类似这样:
如果您填充的文本多于其适合的范围,它将在末尾添加三个点。
如果文本确实太长,只需剪切文本,这样就不会浪费 html 空间。
更多信息请参见:
https://developer.mozilla.org/En/CSS/Text-overflow
在末尾添加“查看更多”链接非常简单,因为附加另一个具有固定宽度的跨度,包含用于查看更多的链接。在此之前,文本将被省略号截断。
Since you are asking for a web page then you can use CSS text-overflow to do that.
It seems to be supported enough, and for firefox there seems to be css workarounds or jquery workarounds...
Something like this:
If you fill more text than it fits it will add the three dots at the end.
Just cut the text if it is really too long so you don't waste html space.
More info here:
https://developer.mozilla.org/En/CSS/Text-overflow
Adding a 'see more' link at the end is easy enough, as appending another span with fixed width, containing the link to see more. text will be truncated with ellipsis before that.