在 (x) 个字符后剪切文本 [英] cut text after (x) amount of characters

查看:22
本文介绍了在 (x) 个字符后剪切文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是在 wordpress 中(不确定是否有所不同)

This is in wordpress (not sure that makes a difference)

这段php输出帖子标题

This bit of php outputs the post title

<?php echo $data['nameofpost']; ?>

它是简单的文本,最长可达 100 个字符.我想要的是输出的字符是否超过 20 长以显示 '...' 或根本没有.

It's simple text which can be anywhere up to 100 chars long. What i'd like is if the chars outputted are over 20 long to display '...' or simply nothing at all.

谢谢

推荐答案

使用 strlen 使用 substr

$string = "This is a large text for demonstrations purposes";
if(strlen($string) > 20) $string = substr($string, 0, 20).'...';
echo $string;

输出

"This is a large text..."

这篇关于在 (x) 个字符后剪切文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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