缩短末尾带有 ... 的字符串
有没有官方的 PHP 函数可以做到这一点?这个动作叫什么?
Is there an official PHP function to do this? What is this action called?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
有没有官方的 PHP 函数可以做到这一点?这个动作叫什么?
Is there an official PHP function to do this? What is this action called?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(4)
不,没有内置函数,但您当然可以构建自己的函数:
No, there is no built-in function, but you can of course build your own:
按单词截断
Truncate by word
不,PHP 没有内置的“截断”字符串函数(除非某个奇怪的扩展有这个函数,但不能保证观众/您将拥有这种插件——我不知道有哪个插件可以做到这一点) 。
我建议只为它编写一个简单的函数,例如:
如果您想使用“暂停点”字符(带有三个点的单个字符;它是 unicode),请使用 HTML 实体
&哎呀;
。No, PHP does not have a function built-in to "truncate" strings (unless some weird extension has one, but it's not guaranteed the viewers/you will have that sort of plugin -- I don't know of any that do).
I would recommend just writing a simple function for it, something like:
And if you'd like to use a "suspension point" character (a single character with the three dots; it's unicode), use the HTML entity
…
.我使用 wordwrap、substr 和 strpos 确保它不会截断单词,或者分隔符前面没有空格。
I use a combination of wordwrap, substr and strpos to make sure it doesn't cut off words, or that the delimiter is not preceded by a space.