How to Find URL in String and Make Link in PHP?

In this post, we will give you very simple example, to find url from string and make anchor tag link using php. it will automatic create link even it’s http, https, ftp and ftps url.

1. Code:

function convertURLs($string){
    $url = '/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/';   
    return preg_replace($url, '<a href="$0" target="_blank" title="$0">$0</a>', $string);

$string = "Hi Soltuts, This is a link: ";
$stringWithUrl = convertURLs($string);
echo($stringWithUrl );

2. Example:


$string = "Hi Soltuts, This is a link: ";


Hi Soltuts, This is a link: