What is the function in PHP used to shorten a string to a specified length?

To shorten a string to a specified length in PHP, you can use the `substr()` function. This function allows you to extract a portion of a string starting from a specified position and with a specified length. By using `substr()`, you can easily truncate a string to a desired length.

$string = "This is a long string that needs to be shortened.";
$shortenedString = substr($string, 0, 20); // Shorten the string to 20 characters
echo $shortenedString; // Output: "This is a long stri"