如何在 PHP 中截断一定数量的字符后的文本?

发布于 2024-07-30 13:28:37 字数 157 浏览 13 评论 0原文

我有两个字符串,我想限制为前 25 个字符。 有没有办法在第 25 个字符之后截断文本并在字符串末尾添加 ... ?

所以'12345678901234567890abcdefg' 会变成“12345678901234567890abcde...”,其中“fg”被切断。

I have two string that i want to limit to lets say the first 25 characters for example. Is there a way to cut off text after the 25th character and add a ... to the end of the string?

So '12345678901234567890abcdefg'
would turn into '12345678901234567890abcde...' where 'fg' is cut off.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

半透明的墙 2024-08-06 13:28:40
$words=explode(' ', $string);
for($ii = 0,$j=0; $ii< 5 && $j < sizeof($words) ;$j++){
    $ii += strlen($words[$j]);
    $intro= $words[$j]. " ";    
}
$intro.= "...";

我知道已经晚了,但如果其他人返回此页面,这样做就可以了,

$words=explode(' ', $string);
for($ii = 0,$j=0; $ii< 5 && $j < sizeof($words) ;$j++){
    $ii += strlen($words[$j]);
    $intro= $words[$j]. " ";    
}
$intro.= "...";

I know its to late, but if anyone else is returning to this page, this will do,

攒一口袋星星 2024-08-06 13:28:40

substr 函数是适合您的函数

substr function is the right one for you

情栀口红 2024-08-06 13:28:40

您正在寻找 substr 方法。

$s = substr($input, 0, 25);

这将为您提供字符串的第一个卡盘,然后您可以将任何您想要的内容附加到末尾。

You're looking for the substr method.

$s = substr($input, 0, 25);

This will get you the first chuck of the string and then you can append whatever you'd like to the end.

靑春怀旧 2024-08-06 13:28:40

我会同意帕斯卡·马丁的答案,并进行一些额外的改进。 因为;

1 - “PHP wordwrap”,自动尊重单词边界

2 - 如果您的字符串包含超过 25 个字符的单词(啊,所以没有什么可做的),您可以强制“PHP wordwrap”剪切单词。(改进)

3 - 如果你的字符串短于25个字符,那么“...”在完整的句子后会显得很难看。(改进)

$str = "This string might be longer than 25 chars, or might not!";
// we are forcing to cut long words...
$wrapped = wordwrap($str, 25, "\n", 1);
var_dump($wrapped);

$lines = explode("\n", $wrapped);
var_dump($lines);

// if our $str is shorter than 25, there will be no $lines[1]
(array_key_exists('1', $lines)) ? $suffix = '...' : $suffix = '';
$new_str = $lines[0] . $suffix;
var_dump($new_str);

I would go with Pascal MARTIN's answer, with some additional improvements. Because;

1 - "PHP wordwrap", respects word boundaries automatically

2 - If your string contains a word more than 25 chars(aagh, so there is nothing to do) you can force "PHP wordwrap" to cut word.(improvement)

3 - If your string is shorter than 25 chars, then "..." will seem ugly after a complete sentence.(improvement)

$str = "This string might be longer than 25 chars, or might not!";
// we are forcing to cut long words...
$wrapped = wordwrap($str, 25, "\n", 1);
var_dump($wrapped);

$lines = explode("\n", $wrapped);
var_dump($lines);

// if our $str is shorter than 25, there will be no $lines[1]
(array_key_exists('1', $lines)) ? $suffix = '...' : $suffix = '';
$new_str = $lines[0] . $suffix;
var_dump($new_str);
花想c 2024-08-06 13:28:40
<?php echo substr('12345678901234567890abcdefg', 0, 20) . '...' ?>

http://fr.php.net/manual/en/function.substr。 php

<?php echo substr('12345678901234567890abcdefg', 0, 20) . '...' ?>

http://fr.php.net/manual/en/function.substr.php

暖阳 2024-08-06 13:28:40
 echo substr($str,0,25)."...";

可以解决这个问题,但是如果你正在处理单词,你可能想要切断单词边界,这样你就不会有部分单词这样做:快速 bl...

所以要做到这一点(从我的顶部粗略地得出)头):

$words = split(" ", strlen($str));

for($i = 0,$j=0; $i< 25 && $j < sizeof($words) ;$j++)
{
    $i += strlen($words[$j]);
    echo $words[$j]. " ";    
}
echo "...";
 echo substr($str,0,25)."...";

Would do the trick, but if you are dealing with words, you might want to cut off on word boundries so you don't have partial word doig this: The quick bl...

So to do that (crude off the top of my head):

$words = split(" ", strlen($str));

for($i = 0,$j=0; $i< 25 && $j < sizeof($words) ;$j++)
{
    $i += strlen($words[$j]);
    echo $words[$j]. " ";    
}
echo "...";
痞味浪人 2024-08-06 13:28:39

为了避免在单词中间剪切,您可能需要尝试 wordwrap功能 ; 我想,这样的事情可以做到:

$str = "this is a long string that should be cut in the middle of the first 'that'";
$wrapped = wordwrap($str, 25);
var_dump($wrapped);

$lines = explode("\n", $wrapped);
var_dump($lines);

$new_str = $lines[0] . '...';
var_dump($new_str);

$wrapped 将包含:

string 'this is a long string
that should be cut in the
middle of the first
'that'' (length=74)

$lines 数组将类似于:

array
  0 => string 'this is a long string' (length=21)
  1 => string 'that should be cut in the' (length=25)
  2 => string 'middle of the first' (length=19)
  3 => string ''that'' (length=6)

最后,你的 $new_string

string 'this is a long string' (length=21)

使用 substr,像这样:

var_dump(substr($str, 0, 25) . '...');

你会得到:

string 'this is a long string tha...' (length=28)

看起来不太好 :-(

不过,玩得开心!

To avoid cutting right in the middle of a word, you might want to try the wordwrap function ; something like this, I suppose, could do :

$str = "this is a long string that should be cut in the middle of the first 'that'";
$wrapped = wordwrap($str, 25);
var_dump($wrapped);

$lines = explode("\n", $wrapped);
var_dump($lines);

$new_str = $lines[0] . '...';
var_dump($new_str);

$wrapped will contain :

string 'this is a long string
that should be cut in the
middle of the first
'that'' (length=74)

The $lines array will be like :

array
  0 => string 'this is a long string' (length=21)
  1 => string 'that should be cut in the' (length=25)
  2 => string 'middle of the first' (length=19)
  3 => string ''that'' (length=6)

And, finally, your $new_string :

string 'this is a long string' (length=21)

With a substr, like this :

var_dump(substr($str, 0, 25) . '...');

You'd have gotten :

string 'this is a long string tha...' (length=28)

Which doesn't look that nice :-(

Still, have fun !

蓝咒 2024-08-06 13:28:39

这个很短并且考虑了字边界,它不使用循环,这使得它非常有效

function truncate($str, $chars, $end = '...') {
    if (strlen($str) <= $chars) return $str;
    $new = substr($str, 0, $chars + 1);
    return substr($new, 0, strrpos($new, ' ')) . $end;
}

用法:

truncate('My string', 5); //returns: My...

This one is short and takes word boundary into account, it doesn't use loops which makes it very efficient

function truncate($str, $chars, $end = '...') {
    if (strlen($str) <= $chars) return $str;
    $new = substr($str, 0, $chars + 1);
    return substr($new, 0, strrpos($new, ' ')) . $end;
}

Usage:

truncate('My string', 5); //returns: My...
月亮是我掰弯的 2024-08-06 13:28:39

http://php.net/manual/en/function.mb-strimwidth.php
(PHP 4 >= 4.0.6、PHP 5、PHP 7)

<?php
echo mb_strimwidth("Hello World", 0, 10, "...");
echo "<br />";
echo mb_strimwidth("Hello", 0, 10, "...");
?>

输出:

Hello W...
Hello

http://php.net/manual/en/function.mb-strimwidth.php
(PHP 4 >= 4.0.6, PHP 5, PHP 7)

<?php
echo mb_strimwidth("Hello World", 0, 10, "...");
echo "<br />";
echo mb_strimwidth("Hello", 0, 10, "...");
?>

output:

Hello W...
Hello
朦胧时间 2024-08-06 13:28:38

我可以修改pallan的代码吗?

$truncated = (strlen($string) > 20) ? substr($string, 0, 20) . '...' : $string;

如果“...”较短,则不会添加它。

May I make a modification to pallan's code?

$truncated = (strlen($string) > 20) ? substr($string, 0, 20) . '...' : $string;

This doesn't add the '...' if it is shorter.

孤独患者 2024-08-06 13:28:38

真的很快,

$truncated = substr('12345678901234567890abcdefg', 0, 20) . '...'

Really quickly,

$truncated = substr('12345678901234567890abcdefg', 0, 20) . '...'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文