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:

<?php
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: https://soltuts.com/ ";
$stringWithUrl = convertURLs($string);
echo($stringWithUrl );
?>

2. Example:

Input:

$string = "Hi Soltuts, This is a link: https://soltuts.com/ ";

Output:

Hi Soltuts, This is a link: https://soltuts.com/