dadamssg Posted April 17, 2009 Share Posted April 17, 2009 when i type in a site... http://www.google.com it makes it a link? anyone point me in the right direction on how to do this? Quote Link to comment https://forums.phpfreaks.com/topic/154545-solved-how-does-this-site-recognize-websites/ Share on other sites More sharing options...
jackpf Posted April 17, 2009 Share Posted April 17, 2009 preg_match() and preg_replace() most probably. Quote Link to comment https://forums.phpfreaks.com/topic/154545-solved-how-does-this-site-recognize-websites/#findComment-812593 Share on other sites More sharing options...
Maq Posted April 17, 2009 Share Posted April 17, 2009 If it matches a regex for a site, then surround it by tags, if not, don't. Do you need help with the regex? Quote Link to comment https://forums.phpfreaks.com/topic/154545-solved-how-does-this-site-recognize-websites/#findComment-812606 Share on other sites More sharing options...
dadamssg Posted April 17, 2009 Author Share Posted April 17, 2009 yeah..more than that if youre willing to help though.i just don't really know how to write regex and then how to use preg_replace or whatever i need :-\ Quote Link to comment https://forums.phpfreaks.com/topic/154545-solved-how-does-this-site-recognize-websites/#findComment-812632 Share on other sites More sharing options...
Maq Posted April 17, 2009 Share Posted April 17, 2009 yeah..more than that if youre willing to help though.i just don't really know how to write regex and then how to use preg_replace or whatever i need :-\ k, moving to regex for further help. Quote Link to comment https://forums.phpfreaks.com/topic/154545-solved-how-does-this-site-recognize-websites/#findComment-812634 Share on other sites More sharing options...
jackpf Posted April 17, 2009 Share Posted April 17, 2009 Probably something similiar to this $url = preg_replace('/http\:\/\/(.*?)/i', '<a href="http://$1">$1</a>', $url); Quote Link to comment https://forums.phpfreaks.com/topic/154545-solved-how-does-this-site-recognize-websites/#findComment-812669 Share on other sites More sharing options...
dadamssg Posted April 18, 2009 Author Share Posted April 18, 2009 so that will run through the content of my variable...its gonna be a long string of text and what-not. and if it sees http:// it will make it a link? Quote Link to comment https://forums.phpfreaks.com/topic/154545-solved-how-does-this-site-recognize-websites/#findComment-812985 Share on other sites More sharing options...
jackpf Posted April 18, 2009 Share Posted April 18, 2009 Theoretically yes. I haven't tested it though. But it is something along the lines of that. Quote Link to comment https://forums.phpfreaks.com/topic/154545-solved-how-does-this-site-recognize-websites/#findComment-813073 Share on other sites More sharing options...
allenskd Posted April 20, 2009 Share Posted April 20, 2009 although i'm not that experienced in regex, here an example that you can play with: <?php $test = 'http://google.com'; $url = preg_replace('#(?:http:[\/]{2})([a-z0-9-_]+)(.[a-z]{2,})#ui', '<a href="http://$1$2">$1</a>', $test); echo $url; sadly, it will ignore further extension of the link (eg. http://google.com/i/want/a/cookie , it will only stop at google.com) Then again, you might want to be careful when creating this. For example in jackpf's post, you can add quotes, ampersand, and other symbols that can harm your site, since its doing a no-holds barred any characters may enter Quote Link to comment https://forums.phpfreaks.com/topic/154545-solved-how-does-this-site-recognize-websites/#findComment-814629 Share on other sites More sharing options...
premiso Posted April 20, 2009 Share Posted April 20, 2009 <?php function convertLinks($text) { $in=array( '`((?:https?|ftp)://\S+[[:alnum:]]/?)`si', '`((?<!//)(www\.\S+[[:alnum:]]/?))`si' ); $out=array( '<a href="$1" >$1</a> ', '<a href="http://$1" >$1</a>' ); return preg_replace($in,$out,$text); } $string = "This is some text http://www.google.com and www.google.com and http://google.com but google.com does not work as clickable."; $string = convertLinks($string); echo $string; ?> Should work. It is nearly impossible to do the google.com without the www. or http:// so yea. Hopefully that works for you. EDIT: As an fyi, I did not write that, I found it in the comments at preg_replace I just removed certain portions and renamed it. Quote Link to comment https://forums.phpfreaks.com/topic/154545-solved-how-does-this-site-recognize-websites/#findComment-814633 Share on other sites More sharing options...
dadamssg Posted April 21, 2009 Author Share Posted April 21, 2009 sweet thanks...but this wouldn't make www.blahblah.com a link would it? it will have to have the http:// right? Quote Link to comment https://forums.phpfreaks.com/topic/154545-solved-how-does-this-site-recognize-websites/#findComment-815514 Share on other sites More sharing options...
dadamssg Posted April 21, 2009 Author Share Posted April 21, 2009 just kidding...i looked at into it more...thanks a LOT premiso, really appreciate it Quote Link to comment https://forums.phpfreaks.com/topic/154545-solved-how-does-this-site-recognize-websites/#findComment-815518 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.