Hi i would like to know how i add the ... when you cut a string in php. For example a string gets cut that exceeds the character limit and gets added with ... You can find this anywhere like on youtube or any big media site. I have searched everywhere but could not find any help. I used the substr function and would like to know if there is any way to use that to get the three dots.

Dani AI

Generated

This thread shows the two common approaches and their pitfalls. asked how to add an ellipsis when a string is shortened; posted a quick cut-and-append function (it contains a parameter bug and isn’t multibyte‑safe), and offered a word-aware routine that avoids splitting words but always appends the dots and doesn’t handle very long single words or UTF‑8 reliably. Below are practical, safer options and a few production tips.

For simple, multibyte‑safe trimming that only adds the marker when truncation occurs, mb_* helpers are preferred:

if (mb_strlen($text, 'UTF-8') > $max) {
    $short = mb_strimwidth($text, 0, $max, '...', 'UTF-8');
} else {
    $short = $text;
}

For keeping whole words, trim to the limit then backtrack to the last space (falls back to the hard cut if there’s no space). This avoids splitting words and removes trailing punctuation before appending the ellipsis:

function ellipsize($text, $max, $enc = 'UTF-8') {
    if (mb_strlen($text, $enc) <= $max) return $text;
    $trunc = mb_substr($text, 0, $max, $enc);
    $pos = mb_strrpos($trunc, ' ', 0, $enc);
    if ($pos !== false) $trunc = mb_substr($trunc, 0, $pos, $enc);
    return rtrim($trunc, " \t\n\r\0\x0B.,;:!?\"')(") . '...';
}

Additional notes useful long after this thread:

  • Use mb_* functions for UTF‑8 text to avoid broken characters.
  • For HTML content, strip or use a DOM‑aware truncator to avoid breaking tags.
  • For UI-only truncation prefer CSS (single line: text-overflow: ellipsis; multi-line: -webkit-line-clamp) so the stored text remains intact.
  • Keep raw text in the database and truncate on output for SEO and flexibility.

Correcting ’s parameter issue and making ’s idea multibyte‑aware and conditional on truncation produces the most robust result in real apps.

Recommended Answers

All 2 Replies

Perhaps the following function?

function trimstr($input,$length) {
if (strlen($input)>$length) {
return substr($input,$length).'...';
} else {
return $input;
}
}

echo trimstr('abcdefghijklmnopqrstuvwxyz',3);

I know that this thread is old but i needed the same thing and found a really nice solution.

$string = substr($string,0,30);

The string variable is now set to the following.

$string = 'This string is too long and wi';

This example highlights the major problem with this function in that it will take no notice of the words in a string. The solution is to create a new function that will avoid cutting words apart when cutting a string by a number of characters.

function substrwords($text,$maxchar,$end='...'){
 if(strlen($text)>$maxchar){
  $words=explode(" ",$text);
  $output = '';
  $i=0;
  while(1){
   $length = (strlen($output)+strlen($words[$i]));
   if($length>$maxchar){
    break;
   }else{
    $output = $output." ".$words[$i];
    ++$i;
   };
  };
 }else{
  $output = $text;
 }
 return $output.$end;
}

This function splits the string into words and then joins them back together again one by one. The good thing is that if there is a word that would make the string longer than the required number of characters it doesn’t include the word. The function can be called like this.

echo substrwords($string,30);

This produces the following output.

This string is too long and...
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.