我的 MySQL 数据库中有一个描述字段,我在两个不同的页面上访问数据库,一个页面显示整个字段,但在另一个页面上,我只想显示前 50 个字符。如果描述字段中的字符串少于 50 个字符,则不会显示 ... ,但如果不是,我将在前 50 个字符之后显示 ... 。
示例(完整字符串):
Hello, this is the first example, where I am going to have a string that is over 50 characters and is super long, I don't know how long maybe around 1000 characters. Anyway this should be over 50 characters now ...
示例 2(前 50 个字符):
Hello, this is the first example, where I am going ...
PHP 的方法很简单:
$out = strlen($in) > 50 ? substr($in,0,50)."..." : $in;
但是你可以使用这个 CSS 实现更好的效果:
.ellipsis {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
现在,假设元素具有固定宽度,浏览器将自动中断并添加...
为你。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)