Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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 !

Really quickly,

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

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...

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
$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,

substr function is the right one for you

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.

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

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

 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 "...";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top