Regex to Create Hyperlinks in a String Using PHP

This is a PHP regex script that identifies URLs in a string and converts them into an HTML href tag.

This is handy to let users free-input data as they like and then use logic on the backend to give them the user experience they expect.

This regex could of course be used in Javascript, python, or any other language.  I’m just sharing the concept here – feel free to adapt and use as needed.

PHP regex Function to parse URLs

Testing The Regex

You can do a live test of this regex at regex101.com.  Feel free to try your own URLs to stress-test the logic here.