Find URLs in a string and make clickable links in PHP

In this short article, we’ll discuss how to find URLs in a string and make clickable links in PHP. Sometimes you have data without any hyperlinks tags and when displayed on a web page you may need to convert the hyperlinks to our page.

As you can often see in Facebook and other social media networks, if there is a URL to the text of a message or comment, it becomes clickable. So here we will learn how to find a url in content and convert to anchor tag link using PHP.

Using built-in PHP function preg_replace(), we can easily find the URLs in text and make links in PHP. The preg_replace() function matches the pattern from string and replaces it with a defined modified string.

You can also check Find URLs in string and make a link using JavaScript.

Here, we take an example to find multiple URLs from a string and convert them to URLs automatically.

In the above example, we have created a basic function that finds the pattern in a string and turns it into clickable links. It will find URLs that contain http, https, ftp, ftps or www.

That’s it for today.
Thank you for reading. Happy Coding..!! 🙂

If you found value in this article,
you can support us by buying me a coffee! ☕

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *