Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在用 mysql 构建一个 RSS 脚本,我不想在数据库中有一个额外的字段......我想用“......阅读更多”缩小文章的主体,但我不确定如何限制回显到页面上的字符数?
当然不是这种语法,而是类似于以下内容:
echo(limit($row['newsBody'], 1000));
我不介意是否需要 15 行代码来执行此操作;)
PS我确定limit()是一个函数,请不要告诉我..它只是一个例子;)
提前致谢!
echo(substr($row['newsBody'], 0, 1000));
该substr功能是您正在寻找的。或者,mb_substr如果您正在处理多字节字符串,则可以使用。
substr
mb_substr
你可以这样做
$body = $row['newsBody']; $length = strlen($body) > 1000 ? 1000 : strlen($1000); echo substr($body,0, $length);
它将打印前 1000 个字符或更短的整个消息
substr( $string, 0, 1000 );