note 74582 deleted from function.preg-replace by colder

From: Date: Mon, 18 Jun 2007 20:11:00 +0000
Subject: note 74582 deleted from function.preg-replace by colder
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-127724@lists.php.net to get a copy of this message
Note Submitter: marten_berglund at hotmail dot com ---- 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>"; } ?>

« previous php.notes (#127724) next »