Created
April 29, 2016 04:40
-
-
Save Aziz-Rahman/915ab8e49a68b401bc71f76cd05a8f96 to your computer and use it in GitHub Desktop.
Detect url
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 !!!