限制字符串字符 - PHP

发布于 2024-08-09 18:57:46 字数 274 浏览 3 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

左岸枫 2024-08-16 18:57:46
echo(substr($row['newsBody'], 0, 1000));

substr 函数就是您要寻找的。或者您可以使用 mb_substr 如果您正在处理多字节字符串。

echo(substr($row['newsBody'], 0, 1000));

The substr function is what you are looking for. Or you can use mb_substr if you are dealing with multi-byte strings.

暗地喜欢 2024-08-16 18:57:46

你可以这样做

$body = $row['newsBody'];
$length = strlen($body) > 1000 ? 1000 : strlen($1000);
echo substr($body,0, $length);

它会打印前 1000 个字符或整个消息,以较短者为准

You could do this

$body = $row['newsBody'];
$length = strlen($body) > 1000 ? 1000 : strlen($1000);
echo substr($body,0, $length);

It'll print the first 1000 characters or the whole message which ever is shorter

躲猫猫 2024-08-16 18:57:46
substr( $string, 0, 1000 );
substr( $string, 0, 1000 );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文