JSHINER Posted July 31, 2007 Share Posted July 31, 2007 <?php $content = "This is the content with a link: http://www.link.com - visit the site!" echo $content; ?> How can I make it so if someone enters a link it makes the address is clickable? And if they only enter www, it includes http:// in the href. Quote Link to comment https://forums.phpfreaks.com/topic/62668-solved-making-a-clickable-link-from-a-variable/ Share on other sites More sharing options...
wildteen88 Posted July 31, 2007 Share Posted July 31, 2007 You will want to use regular expressions using preg_replace. Have a search through the Regex Help forum. I'm pretty sure there has been question like yours already posted within that board. Quote Link to comment https://forums.phpfreaks.com/topic/62668-solved-making-a-clickable-link-from-a-variable/#findComment-311898 Share on other sites More sharing options...
mrjcfreak Posted July 31, 2007 Share Posted July 31, 2007 A quick glance at the php.net manual reveals this: <?php function hyperlink(&$text) { // match protocol://address/path/ $text = ereg_replace("[a-zA-Z]+://([.]?[a-zA-Z0-9_/-])*", "<a href=\"\\0\">\\0</a>", $text); // match www.something $text = ereg_replace("(^| |.)(www([.]?[a-zA-Z0-9_/-])*)", "\\1<a href=\"http://\\2\">\\2</a>", $text); } ?> Source: http://uk2.php.net/manual/en/function.ereg-replace.php#75448 Quote Link to comment https://forums.phpfreaks.com/topic/62668-solved-making-a-clickable-link-from-a-variable/#findComment-311899 Share on other sites More sharing options...
JSHINER Posted July 31, 2007 Author Share Posted July 31, 2007 Searched, found the solution. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/62668-solved-making-a-clickable-link-from-a-variable/#findComment-311908 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.