How can we make links clickable in a string using PHP?

To make links clickable in a string using PHP, we can use the preg_replace function to search for URLs within the string and replace them with clickable HTML anchor tags. We need to use a regular expression to identify URLs in the string and then wrap them with the <a> tag to create clickable links.

function makeLinksClickable($text) {
    return preg_replace(&#039;/(https?:\/\/[^\s]+)/&#039;, &#039;&lt;a href=&quot;$1&quot; target=&quot;_blank&quot;&gt;$1&lt;/a&gt;&#039;, $text);
}

// Example usage
$text = &quot;Check out this website: https://www.example.com&quot;;
echo makeLinksClickable($text);