在 PHP 中,如何在一定数量的字符后截断文本? [英] How do you cut off text after a certain amount of characters in PHP?

查看:21
本文介绍了在 PHP 中,如何在一定数量的字符后截断文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个字符串,我想限制为例如前 25 个字符.有没有办法在第 25 个字符之后截断文本并在字符串的末尾添加一个 ...?

I have two string that i want to limit to lets say the first 25 characters for example. Is there a way to cut off text after the 25th character and add a ... to the end of the string?

所以'12345678901234567890abcdefg'将变成 '12345678901234567890abcde...' 其中 'fg' 被切断.

So '12345678901234567890abcdefg' would turn into '12345678901234567890abcde...' where 'fg' is cut off.

推荐答案

我可以修改 pallan 的代码吗?

May I make a modification to pallan's code?

$truncated = (strlen($string) > 20) ? substr($string, 0, 20) . '...' : $string;

如果较短,则不会添加...".

This doesn't add the '...' if it is shorter.

这篇关于在 PHP 中,如何在一定数量的字符后截断文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆