What are the recommended best practices for converting URLs in text to clickable links in PHP to ensure proper functionality and user experience?

When converting URLs in text to clickable links in PHP, it is important to use the `preg_replace` function with a regular expression to match URLs in the text. This ensures that only valid URLs are converted to clickable links. Additionally, it is recommended to use the `target="_blank"` attribute in the anchor tag to open the link in a new tab for better user experience.

function convertUrlsToLinks($text) {
    return preg_replace('/\b(?:https?|ftp):\/\/[^\s+]+\b/', '<a href="$0" target="_blank">$0</a>', $text);
}

$text = "Check out this website: https://www.example.com";
echo convertUrlsToLinks($text);