How can the title attribute be utilized to display the full URL while showing a truncated version for better readability in PHP?

To display the full URL while showing a truncated version for better readability in PHP, you can utilize the title attribute in HTML. By setting the title attribute to the full URL and displaying a truncated version of the URL as the link text, users can hover over the link to view the complete URL. This provides a balance between readability and accessibility of the full URL.

<?php
$fullURL = "https://www.example.com/this-is-a-long-url-that-needs-to-be-truncated-for-readability";
$truncatedURL = (strlen($fullURL) > 30) ? substr($fullURL, 0, 30) . '...' : $fullURL;
echo '<a href="' . $fullURL . '" title="' . $fullURL . '">' . $truncatedURL . '</a>';
?>