Skip to content

Instantly share code, notes, and snippets.

@Aziz-Rahman
Created April 29, 2016 04:40
Show Gist options
  • Save Aziz-Rahman/915ab8e49a68b401bc71f76cd05a8f96 to your computer and use it in GitHub Desktop.
Save Aziz-Rahman/915ab8e49a68b401bc71f76cd05a8f96 to your computer and use it in GitHub Desktop.
Detect url
// Detect url
if (!function_exists('parse_links')) {
function parse_links($str) {
// The Regular Expression filter
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?|[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
// Check if there is a url in the text
if(preg_match($reg_exUrl, $str, $url)) {
// make the urls hyper links
return preg_replace($reg_exUrl, '<a href="'.$url[0].'" rel="nofollow">'.$url[0].'</a>', $str);
} else {
// if no urls in the text just return the text
return $str;
}
}
}
// Display
echo parse_links( $text );
@Aziz-Rahman
Copy link
Author

Example:
$text = "The text you want to filter goes here https://css-tricks.com/snippets/php/find-urls-in-text-make-links/ yayaya";
echo parse_links( $text );

Enjoy life !!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment