What function can be used to truncate a string in PHP?

To truncate a string in PHP, you can use the `substr()` function. This function allows you to extract a portion of a string based on the specified start position and length. By using `substr()` with the desired length, you can effectively truncate the string to the desired length.

$string = "This is a long string that needs to be truncated.";
$truncatedString = substr($string, 0, 20); // Truncate the string to 20 characters

echo $truncatedString; // Output: "This is a long stri"