Source Code:
(back to article)
<?php function truncate($string, $length) { if (strlen($string) > $length) { return substr($string, 0, $length - 3) . '...'; } return $string; } $string = 'This is a long string'; echo truncate($string, 10); // Outputs: "This is ..." echo truncate($string, 20); // Outputs: "This is a long string"
Result:
Report an issue