缩短PHP中的文本字符串 [英] Shorten a text string in PHP

查看:273
本文介绍了缩短PHP中的文本字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以在PHP中修剪文本字符串,使其具有一定数量的字符?例如,如果我有字符串:

Is there a way to trim a text string in PHP so it has a certain number of characters? For instance, if I had the string:

$string = "this is a string";

我怎么剪成这样:

$newstring = "this is";

这是我到目前为止使用chunk_split()所获得的,但是它不起作用.谁能改善我的方法?

This is what I have so far, using chunk_split(), but it isn't working. Can anyone improve on my method?

function trimtext($text)
{
$newtext = chunk_split($text,15);
return $newtext;
}

我还查看了这个问题,但我不太了解.

I also looked at this question, but I don't really understand it.

推荐答案

if (strlen($yourString) > 15) // if you want...
{
    $maxLength = 14;
    $yourString = substr($yourString, 0, $maxLength);
}

会做.

此处.

这篇关于缩短PHP中的文本字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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