首先,Dedecms提供了两个非常实用的字符串处理函数:
Html2text() // 去掉HTML标签,仅保留纯文本内容。
cn_substr(str, length) // 截取指定长度的字符串,length为截取字符数。
[field:body function="cn_substr(Html2text(@me),80)" /]
接下来,我们再来看一些其他常用的函数总结:
1. cn_substr(@me, length)
功能:获取指定长度的字符串。
2. html2text(@me)
功能:去掉HTML样式,转换为纯文本字符。
3. GetDateTimeMk('time')
功能:根据秒数返回时间。
4. GetDateMK(time)
功能:根据秒数返回日期。
5. strftime(format, time)
功能:根据秒数返回格式化的日期或时间(PHP自带函数)。
以下是首页中常用的时间格式化示例:
[field:pubdate function=strftime('%d',@me)/] // 输出如:08
[field:pubdate function=strftime('%d日',@me)/] // 输出如:08 日
[field:pubdate function=strftime('%m-%d',@me)/] // 输出如:06-08
[field:pubdate function=strftime('%m月%d日',@me)/] // 输出如:06月08日
[field:pubdate function=strftime('%y-%m-%d',@me)/] // 输出如:09-06-08
[field:pubdate function=strftime('%Y-%m-%d',@me)/] // 输出如:2009-06-08
[field:pubdate function=strftime('%y年%m月%d日',@me)/] // 输出如:09年06月08日
[field:pubdate function=strftime('%Y年%m月%d日',@me)/] // 输出如:2009年06月08日
[field:pubdate function=strftime('%Y-%m-%d %H:%M',@me)/] // 输出如:2009-06-08 13:28
[field:pubdate function="GetDateTimeMK(@me)"/] // 输出如:2008-1-1 18:30:02
[field:pubdate function="GetDateMK(@me)"/] // 输出如:2008-05-15
1. str_replace(search, replace, subject)
功能:替换字符串中的指定内容。
2. MyDate(format, time)
功能:返回格林威治标准时间。
3. html2text(cn_substr(@me, length))
功能:提取指定个数的字符串并去掉HTML样式,转换为纯文本字符。
最后,通过一个具体的例子来展示如何使用这些函数:
{dede:field.content function="Html2Text(cn_substr('@me',110))" /}
通过以上介绍,相信您已经掌握了Dedecms中字符串截取及时间格式化的相关技巧。希望这些内容能够对您的开发工作有所帮助!