Skip to content

Instantly share code, notes, and snippets.

@karmiphuc
Last active January 3, 2016 20:39
Show Gist options
  • Save karmiphuc/8516566 to your computer and use it in GitHub Desktop.
Save karmiphuc/8516566 to your computer and use it in GitHub Desktop.
URL Parsing from text
<?
function parseUrl($s) {
$__ = preg_replace('/([A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&~\?\/.=]+)/i', '<a href="$1" target="_blank">$1</a>', $s);
return preg_replace('/(^| )([^\/])((www|[^\/\. ]+)\.[A-Za-z]+(\b|$|))/i', '$1<a href="http://$2$3" target="_blank">$2$3</a>', $__);
};
$s = 'Tinhte.vn asdasd google.com asdjaskdjajks www.abc.com xxx Google http://google.com.vm/search/asdajs.html';
var_dump(parseUrl($s));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment