如何用php截取一定长度的文本?
我想从数据库中获取一些字段值并将它们呈现在 html 上。
但其中一些比 div 宽度长,所以我想砍掉 if 并在它们后面添加 3 个点(如果它们比 30 个字符长)。
windows vs mac os x-> windows vs m...
threads about windows vista -> threads about win...
我怎样才能做到这一点?
i wanna get some field values from database and present them on html.
but some of them are longer than the div width so i wanted to chop if of and add 3 dots after them if they are longer than lets say 30 characthers.
windows vs mac os x-> windows vs m...
threads about windows vista -> threads about win...
how can i do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您需要多次执行此类功能,请考虑此功能:
用法:
If you need to perform this kind of functionality more than once, consider this function:
Usage:
从你的例子来看,你似乎并不关心保留单词,所以这里是:
Judging by your examples you don't seem to care about preserving words, so here it is:
如果您使用Smarty,则可以使用截断 修饰符。
顺便说一句,这种功能应该存在于任何像样的模板引擎中。
If you use Smarty, you can use the truncate modifier.
BTW, such kind of function should exist in any decent template engine.
查看 wordwrap(),这应该就是您要查找的内容为了。
Check out wordwrap(), that should be what you're looking for.