Home > php教程 > php手册 > 如何在把超过固定长度的文字以"……"表示

如何在把超过固定长度的文字以"……"表示

WBOY
Release: 2016-06-13 10:04:41
Original
948 people have browsed it

有次,有个以前的同事(是个小MM)问我一个问题:她想在一个网页中显示一个文章标题的列表,但是考虑到有的标题太长,影响美观,就想参照别的网站上的样式,把超过固定长度的文字用省略号代替,但是自己又不知道如何实现,所以想到我(唉,需要我的时候才会想到我,呜……)
拿到题目后,我决定从数据库来着手,写查询语句来实现,结果如下:
说明语句:
SELECT (LEFT(原字段,位数) '...') AS 新字段
FROM 表名 WHERE DATALENGTH(原字段)>位数
UNION ALL
SELECT 原字段 AS 新字段 FROM 表名 WHERE DATALENGTH(原字段)后来考虑到ACCESS中不支持DATALENGTH()函数,所以改为LEN(),不过这样的话,中文字也算作一位,而不是原来的两位。
最后写成:
select top 5 * from(
SELECT id,(LEFT([description],25) '...') AS descriptionx,kind,datetime, description, author, hit FROM [xjx] WHERE len(description)>25
UNION ALL
SELECT id,description AS descriptionx,kind,datetime, description, author, hit FROM [xjx] WHERE len(description)

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template