$string = "The quick brown fox jumps over the lazy dog."; $trimmedString = truncate($string, 20, "..."); echo $trimmedString; // Output: "The quick brown fox..."
$longString = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed imperdiet justo eu sollicitudin feugiat."; $shortString = truncate($longString, 30, "..."); echo $shortString; // Output: "Lorem ipsum dolor sit amet..."In this example, the long string is truncated to a length of 30 characters and the suffix is appended using the truncate function. The truncate() function is part of the PHP string library and does not require any additional package/library to be installed.