19

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.

jiexi
  • 2,959
  • 7
  • 23
  • 27
  • You might find [`s($str)->truncate(25)`](https://github.com/delight-im/PHP-Str/blob/8fd0c608d5496d43adaa899642c1cce047e076dc/src/Str.php#L233) or [`s($str)->truncateSafely(25)`](https://github.com/delight-im/PHP-Str/blob/8fd0c608d5496d43adaa899642c1cce047e076dc/src/Str.php#L246) helpful, as found in [this standalone library](https://github.com/delight-im/PHP-Str). Both variants are Unicode-safe and the second avoids breaking any words. – caw Jul 27 '16 at 02:33

11 Answers11

45

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.

Chains
  • 11,681
  • 8
  • 40
  • 61
Tyler Carter
  • 57,335
  • 20
  • 122
  • 148
  • 1
    i know that this answer is 7 years old but all the people who are looking answer to this question need to be aware that if you using utf8 encodind you need to replace strlen function with mb_strlen and substr function with mb_substr – Edgars Aivars Sep 16 '17 at 15:10
10

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 !

Pascal MARTIN
  • 374,560
  • 73
  • 631
  • 650
8

Really quickly,

$truncated = substr('12345678901234567890abcdefg', 0, 20) . '...'
Peer Allan
  • 3,616
  • 2
  • 18
  • 16
  • can you tell me how to only add the ... if it is past 20 characters? – jiexi Aug 06 '09 at 20:34
  • See this answer: http://stackoverflow.com/questions/1241224/how-do-you-cut-off-text-after-a-certain-amount-of-characters-in-php/1241263#1241263 – Tyler Carter Aug 06 '09 at 20:36
7

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...
Arne
  • 5,581
  • 2
  • 19
  • 18
5

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
eduardogoncalves
  • 131
  • 1
  • 10
2
$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,

MeeDNite
  • 21
  • 1
1

substr function is the right one for you

usoban
  • 5,225
  • 23
  • 39
1

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.

ahawker
  • 3,186
  • 22
  • 23
1

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);
szsahin
  • 11
  • 5
0
<?php echo substr('12345678901234567890abcdefg', 0, 20) . '...' ?>

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

Vincent Robert
  • 33,284
  • 13
  • 77
  • 115
0
 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 "...";
Byron Whitlock
  • 49,611
  • 27
  • 114
  • 164