Skip to content

Instantly share code, notes, and snippets.

@Leask
Created July 19, 2013 11:04
Show Gist options
  • Save Leask/6038374 to your computer and use it in GitHub Desktop.
Save Leask/6038374 to your computer and use it in GitHub Desktop.
Replacing URLs
<?php
/**
* Replacing URLs
* @param: string.
* @return: string.
*/
function ParseURL($str) {
return preg_replace([
"/(?<=[^\]A-Za-z0-9-=\"'\\/])(https?|ftp|gopher|news|telnet|mms){1}:\/\/([A-Za-z0-9\/\-_+=.~!%@?#%&;:$\\()|]+)/is",
//"/([\n\s])www\.([a-z0-9\-]+)\.([A-Za-z0-9\/\-_+=.~!%@?#%&;:$\[\]\\()|]+)((?:[^\x7f-\xff,\s]*)?)/is",
"/([^\/\/])www\.([a-z0-9\-]+)\.([A-Za-z0-9\/\-_+=.~!%@?#%&;:$\[\]\\()|]+)((?:[^\x7f-\xff,\s]*)?)/is",
"/(?<=[^\]A-Za-z0-9\/\-_.~?=:.])([_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,4}))/si"
], [
"<a href=\"\\1://\\2\" target=\"_blank\">\\1://\\2</a>",
"\\1<a href=\"http://www.\\2.\\3\\4\">[url]www.\\2.\\3\\4[/url]</a>",
"<a href=\"mailto:\\0\">\\0</a>"
], ' '.$str);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment