anthropos9 Posted June 13, 2007 Share Posted June 13, 2007 Hi, I want to be able to make a script that identifies a url that is in plain text and automatically make it into a link. It has to be able to identify a url that begins with either http:// or just www. I've looked all over for a script like this - and I know that I've seen one somewhere, but I can't seem to find one now. I'd make one myself, but I don't know where to start - so that kind of advice would also be welcome. Thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/55456-auto-link-generator-help/ Share on other sites More sharing options...
Wildbug Posted June 13, 2007 Share Posted June 13, 2007 You can use a regular expression. Search the regex subforum on this site for URL validation or something. I know this has come up before. Quote Link to comment https://forums.phpfreaks.com/topic/55456-auto-link-generator-help/#findComment-274074 Share on other sites More sharing options...
anthropos9 Posted June 13, 2007 Author Share Posted June 13, 2007 the regex validation is good, but it needs to identify the url from within a paragraph without any user input telling it $x is the url that you need to make into a link. for instance, in the paragraph: You can find more information www.somesite.com, because that's the only place they actually have any information on this insane topic. the www.somesite.com should automatically be found and made into the html <a href="http://www.somesite.com">www.somesite.com</a> (Just like what happens on this site, since in the post it was turned into a link on its own.) Quote Link to comment https://forums.phpfreaks.com/topic/55456-auto-link-generator-help/#findComment-274081 Share on other sites More sharing options...
per1os Posted June 13, 2007 Share Posted June 13, 2007 marten_berglund at hotmail dot com 18-Apr-2007 11:20 I couldn't find any url2html-solution here that met all these three criteria: 1) Make links of standard web-urls (http://some.com), urls without http (some.com) and emails 2) Urls from all possible protocols (not just http and ftp) 3) To not link delimiter characters surrounding a link, e.g. "My site (www.site.com) is good!" The following code takes care of all this. It is easy to below add extra characters that you don't want in the beginning/end of a link, and to add extra abbreviations that should not be linked (below adopted for some common english ones). By disallowing digits in the end of an url, time stamps like 04.57 won't be linked. It seems to work fine with almost any imaginable url but please feel free to make corrections. <?php function linkify($text) { $strip_lchrs = "[^\s\(\)\[\]\{\}\.\,\;\:\?\!]"; //Not these chars in beginning $strip_rchrs = "[^\s\(\)\[\]\{\}\.\,\;\:\?\!\d]"; //Not these chars in end $prot_pat = $strip_lchrs . "[\S]*\:\/\/[\S]*\.[\S]*" . $strip_rchrs; //abc://de.fg $email_pat = $strip_lchrs . "[\S]*@[\S]*\.[\S]*" . $strip_rchrs; //abc@de.fg $general_pat = $strip_lchrs . "[\S]*\.[\S]*" . $strip_rchrs; //abc.de $preg_pattern = "/(" . $prot_pat . "|" . $email_pat . "|" . $general_pat . ")/ei"; $preg_replace = "check_preg('$1')"; return preg_replace($preg_pattern, $preg_replace, $text); } function check_preg($subpat) { //These abbr. should not be linked $exc_string = "/e\.g|i\.e|et\.c/i"; //If it says abc://de.fg if (preg_match("/[\S]*\:\/\/[\S]*\.[\S]*/i", $subpat) == 1) { return "<a href='" . $subpat . "' target='_top'>" . $subpat . "</a>"; } //If it says abc@de.fg else if (preg_match("/[\S]*@[\S]*\.[\S]*/i", $subpat) == 1) { return "<a href='mailto:" . $subpat . "'>" . $subpat . "</a>"; } //If it says "e.g." don't link else if (preg_match($exc_string, $subpat) == 1) { return $subpat; } //If it says abc.de else return "<a href='http://" . $subpat . "' target='_top'>" . $subpat . "</a>"; } ?> Obviously you were not looking in the right spot. Came straight from the php.net manual off of the preg_replace users comment section. I have not tested, but I am sure it works great. Quote Link to comment https://forums.phpfreaks.com/topic/55456-auto-link-generator-help/#findComment-274093 Share on other sites More sharing options...
anthropos9 Posted June 13, 2007 Author Share Posted June 13, 2007 Thanks. I new I was missing something rather obvious. Quote Link to comment https://forums.phpfreaks.com/topic/55456-auto-link-generator-help/#findComment-274108 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.