How can regular expressions (regex) be used in PHP to improve the functionality of link conversion in a text?

When converting text to HTML, it's common to convert plain text URLs into clickable links. Using regular expressions in PHP can help identify URLs within the text and convert them into HTML links automatically. This can improve the functionality of link conversion in a text by making it easier for users to navigate to external websites.

$text = "Check out this cool website: https://www.example.com";
$pattern = '/(https?:\/\/[^\s]+)/';
$replacement = '<a href="$1" target="_blank">$1</a>';
$converted_text = preg_replace($pattern, $replacement, $text);

echo $converted_text;