Hi guys, I want to do something like youtube where when you type a url in the description it will automatically turn it into a hyperlink. Any advice on getting started?
try this: function parse_links($str) { $str = str_replace('www.', 'http://www.', $str); $str = preg_replace('|http://([a-zA-Z0-9-\./]+)|', '<a href="http://$1">$1</a>', $str); $str = preg_replace('/(([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6})/', '<a href="mailto:$1">$1</a>', $str); return $str; } ?> <span id="more-50"></span> Code (markup):
That won't work if they post a url with just http:// or with http://www. because the http://www. will turn into http://http://www. try this... $str = $variable; //where $variable is the variable of the post or description $str = str_replace('http://', 'www.', $str); $str = str_replace('http://www.', 'www.', $str); $str = str_replace('www.', 'http://www.', $str); $str = preg_replace('|http://([a-zA-Z0-9-\./]+)|', '<a href="http://$1">$1</a>', $str); $str = preg_replace('/(([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6})/', '<a href="mailto:$1">$1</a>', $str); $str = trim($str); echo "".$str.""; //prints the variable of the post with the new link added. Code (markup): Only problem is that if they don't have www. or http:// or http://www. or if they have https:// or another link it will not work sorry I can't test it at the moment I am on vacation and using my phone...don't tell.
This is a nice code. it took me 5 minutes to add it to my script. AustinQPT definitely saved me a lot of hours.