Skip to content

Instantly share code, notes, and snippets.

@farshidrezaei
Last active May 16, 2019 22:32
Show Gist options
  • Save farshidrezaei/834c522abcc2bb282cfca2393a2a37c9 to your computer and use it in GitHub Desktop.
Save farshidrezaei/834c522abcc2bb282cfca2393a2a37c9 to your computer and use it in GitHub Desktop.
Find URLs in String, Make Links
<?php
// The Regular Expression filter
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
// The Text that you want to filter for urls and links
$text = "The text that you want to filter is: https://github.com";
// Check if there is a url in the text
if(preg_match($reg_exUrl, $text, $url)) {
// make the urls hyper links
echo preg_replace($reg_exUrl, "<a href="{$url[0]}">{$url[0]}</a> ", $text);
} else {
// if no urls in the text just return the text
echo $text;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment