PHP - 将字符串切片到一定数量的字符以下
我的问题是我需要将字符串削减到少于 30 个字符,同时还要确保字符串以单词结尾。
我一直在使用这个:
$slidedescription = substr($slidedescription,0,30).'...';
问题是它可以切入字符串中间的单词。有没有什么简单的方法可以确保它完成一个单词但长度不超过 30 个字符?
Possible Duplicate:
How to get first x chars from a string, without cutting off the last word?
php trim a string
My issue is that i need cut a string down to less than 30 characters whilst also making sure that the string finishes on a word.
I have been using this:
$slidedescription = substr($slidedescription,0,30).'...';
The problem is that it can cut into the string mid word. Is there any simple way to make sure that it finished on a word but it less than 30 characters long?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可能会有很多意想不到的情况,我试图尽可能多地涵盖它们:
There can be many unexpected situations i tried to cover as many as i can them up:
查找出现空格的最后一个位置
尝试使用strrpos
您可以 就像这样:
you could try looking for the last position where a space occurs using
strrpos
you could use it like so :