Created
March 27, 2022 05:56
-
-
Save Sharifur/73dc3563923bc3940cb5828e2d063957 to your computer and use it in GitHub Desktop.
parse link from string and replace them as anchor tag
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
function make_links($str){ | |
$pattern = '(?xi)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))'; | |
return preg_replace_callback("#$pattern#i", function($matches) { | |
$input = $matches[0]; | |
$url = preg_match('!^https?://!i', $input) ? $input : "http://$input"; | |
return '<a href="' . $url . '" rel="nofollow" target="_blank">' . "$input</a>"; | |
}, $str); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment