限制字符串字符 - PHP
我正在使用 mysql 构建一个 RSS 脚本,并且我不想在数据库中添加额外的字段...我想用“...阅读更多”缩小文章的正文,但我不确定如何限制页面上回显的字符数?
当然不是这个语法,而是类似的内容:
echo(limit($row['newsBody'], 1000));
我不介意是否需要 15 行代码来完成此操作;)
PS 我确信 limit() 是一个函数,请不要告诉我..这只是一个例子; )
提前致谢!
I am building an RSS script with mysql, and i dont want an extra field in the database... i want to shrink down the body of the article with a "... Read More" but im not sure how i can limit the number of chars echoed out onto the page?
Of course not this syntax, but something along the lines of:
echo(limit($row['newsBody'], 1000));
I dont mind if it takes 15 lines of code to do this ;)
P.S. I am sure limit() is a function, please dont tell me .. its just an example ;)
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
substr
函数就是您要寻找的。或者您可以使用mb_substr
如果您正在处理多字节字符串。The
substr
function is what you are looking for. Or you can usemb_substr
if you are dealing with multi-byte strings.你可以这样做
它会打印前 1000 个字符或整个消息,以较短者为准
You could do this
It'll print the first 1000 characters or the whole message which ever is shorter