DragonFire-N[R] Posted April 17, 2010 Share Posted April 17, 2010 How would I go about automatically adding <a> tags to people's posts when they type in a URL? Quote Link to comment https://forums.phpfreaks.com/topic/198882-auto-hyperlink/ Share on other sites More sharing options...
oni-kun Posted April 17, 2010 Share Posted April 17, 2010 You would use Regular expressions (RegEx), with the function preg_replace. Here is a somewhat simple example I wrote, it should work fine: <?php $text="www.phpfreaks.com is a great site, Look at www.phpfreaks.com/index.php?&foo=bar#baz"; $text = preg_replace( "/(?:^|\b)((((http|https|ftp):\/\/)|(www\.))([\w\.]+)([,:%#&\/?=\w+\.-]+))(?:\b|$)/is", "<a href=\"$1\" target=\"_blank\">$1</a>", $text); print $text; ?> Quote Link to comment https://forums.phpfreaks.com/topic/198882-auto-hyperlink/#findComment-1043958 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.