View Single Post
Old 04-12-2022, 07:13 PM  
vdbucks
Monger Cash
 
Industry Role:
Join Date: Jul 2010
Posts: 2,773
not sure why you have that broken down into 12 lines, nonetheless...

Code:
<?php
  $output = strlen($input) > 50 ? substr($input,0,50)."..." : $input;
  echo $output
?>
That's all you really need.

Function method:

Code:
<?php
  $x = "This is a test of the emergency broadcast system. This is only a test. If this were a real emergency, you'd be dead";
  $length = 50;

  function truncate_text($x, $length) {
    $output = strlen($x) > $length ? substr($x,0,$length)."..." : $x;
    return $output;
  }

  echo truncate_text($x, $length);
?>
Adapt to your use case, obv.

Granted, this is a really archaic way of handling it given the fact you should be truncating by whole words, not single characters. The above example illustrates why (returned string is for the function above is "This is a test of the emergency broadcast system. ..."
vdbucks is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote