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