Home > Tips & Tricks > Find URLs in Text and Make Links with PHP

Find URLs in Text and Make Links with PHP

In this trick, we will find the URLs in text and make links with PHP code script. You may have seen that sometimes the article has URLs as a normal string, not as a hyperlink. So you can convert them using this PHP trick.

To make a PHP snippet code, we will use a regex expression and preg_match() function to find the URLs in text and then get the value from the array and make it links.

Here is the regular expression filter for finding the URLs in text.

Now we will use the preg_match() function to match the above expression filter within the text. If the pattern is found in the text then it will return 1 otherwise 0.

If the above condition will return true and then we will print the text with the link else print the text as it is. See the full code snippet below.

The above script will convert the text to links that have http, https, ftp, and ftps pattern matching with the regex filter.

Being Tricky 😉

Photo of author

About Aman Mehra

Hey there! I'm Aman Mehra, a full-stack developer with over six years of hands-on experience in the industry. I've dedicated myself to mastering the ins and outs of PHP, WordPress, ReactJS, NodeJS, and AWS, so you can trust me to handle your web development needs with expertise and finesse. In 2021, I decided to share my knowledge and insights with the world by starting this blog. It's been an incredible journey so far, and I've had the opportunity to learn and grow alongside my readers. Whether you're a seasoned developer or just dipping your toes into the world of web development, I'm here to provide valuable content and solutions to help you succeed. So, stick around, explore the blog, and feel free to reach out if you have any questions or suggestions. Together, let's navigate the exciting world of web development!

Leave a Comment