immanuelx2 Posted June 27, 2009 Share Posted June 27, 2009 Hey guys, wondering if someone could point me to a sample preg_replace() function that searches for an URL of any kind and encloses it in tags The one I found so far is <?php $text = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', '<a href="$1">$1</a>', $text); but for some reason, if the URL contains a dash "-" it thinks the URL has ended.. ??? Thanks in advance Quote Link to comment Share on other sites More sharing options...
.josh Posted June 28, 2009 Share Posted June 28, 2009 kind of simple but it should do the trick most of the time....idea here is that if something starts with http:// or https:// then it's probably a url, so it just grabs everything up to the first space it finds. The theory being that a url would not have a space in it (or it would be urlencoded so it wouldn't be a physical space char) $text = preg_replace('~(https?://[^\s]+)~i','<a href= "$1">$1</a>',$text); Quote Link to comment Share on other sites More sharing options...
immanuelx2 Posted June 28, 2009 Author Share Posted June 28, 2009 Thank you so very much, worked beautifully!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.