The Little Guy Posted March 27, 2008 Share Posted March 27, 2008 I would like to convert: http://somewebsite.com to <a href="http://somewebsite.com">http://somewebsite.com</a> and leave these alone: <a href="http://somewebsite.com">http://somewebsite.com</a> <a href="http://somewebsite.com">My Web Site</a> I just need to turn the non-clickable URL's to Clickable URL's Quote Link to comment Share on other sites More sharing options...
discomatt Posted March 27, 2008 Share Posted March 27, 2008 <?php $subject = '<a href="http://somewebsite.com">http://somewebsite.com</a> or another example is this should work http://somewebsite.com okay and just to make usre <a href="http://somewebsite.com">http://somewebsite.com</a>'; $result = preg_replace('%(?<!(?:href=")|(?:">))(http://[^\s]++)%', '<a href="$1">$1</a>', $subject); echo $result; ?> That is a very loose example, and should be tightened up a bit better for production use. In english, it finds anything that matches http:// and then grabs everything until a whitespace character shows up (linebreak, space, tab, ect). It then makes sure that before the string, there's no href=" or "> before it... So if you entered <a href="somesite.php">This is http://someurl.com</a> the http://someurl.com would get parsed and converted. It's very VERY tricky to efficiently match against every possible entry. This should cover the basics though. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted March 27, 2008 Author Share Posted March 27, 2008 what if it finds this: http://somewebsite.com, http://somewebsite2.com,http://somewebsite3.com will It parse the comma(s) or 2 url's as one? Quote Link to comment Share on other sites More sharing options...
effigy Posted March 28, 2008 Share Posted March 28, 2008 The same concept is used here. Let me know if you need further assistance. 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.