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 😉