note 36370 added to function.ereg-replace

From: Date: Tue, 07 Oct 2003 18:41:40 +0000
Subject: note 36370 added to function.ereg-replace
Groups: php.notes 
Request: Send a blank email to php-notes+get-57973@lists.php.net to get a copy of this message
Another Replace URLs with links function. Adapted from previous notes. It solves many bugs by lifting existing links before activating non-linked urls. Enjoy. function html_activate_urls($str) { // lift all links, images and image maps preg_match_all("/<a [^>]+>.*<\/a>/is", $str, $matches, PREG_SET_ORDER); foreach($matches as $match) { $key = "<" . md5($match[0]) . ">"; $search[] = $key; $replace[] = $match[0]; } preg_match_all("/<map [^>]+>.*<\/map>/is", $str, $matches, PREG_SET_ORDER); foreach($matches as $match) { $key = "<" . md5($match[0]) . ">"; $search[] = $key; $replace[] = $match[0]; } preg_match_all("/<img [^>]+>/is", $str, $matches, PREG_SET_ORDER); foreach($matches as $match) { $key = "<" . md5($match[0]) . ">"; $search[] = $key; $replace[] = $match[0]; } $str = str_replace($replace, $search, $str); // indicate where urls end if they have these trailing special chars $sentinals = array("'&(quot|#34);'i", // Replace html entities "'&(lt|#60);'i", "'&(gt|#62);'i", "'&(nbsp|#160);'i", "'&(iexcl|#161);'i", "'&(cent|#162);'i", "'&(pound|#163);'i", "'&(copy|#169);'i", "'&#(\d+);'e"); $str = preg_replace($sentinals, "^^sentinal^^\\0^^sentinal^^", $str); $vdom = "[:alnum:]"; // Valid domain chars $vurl = $vdom."_~-"; // Valid subdomain and path chars //$vura = "À-ßà-ÿ!#$%&*+,;=@.".$vurl; // Valid additional parameters (after '?') chars; $vura = "À-ßà-ÿ!#$%&*+,;=@./".$vurl; // Valid additional parameters (after '?') chars; // insert other local characters if needed $protocol = "[[:alpha:]]{3,10}://"; // Protocol exp $server = "([$vurl]+[.])*[$vdom]+"; // Server name exp $path = "(([$vurl]+([.][$vurl]+)*/)|([.]{1,2}/))*"; // Document path exp (/.../) $name = "[$vurl]+([.][$vurl]+)*"; // Document name exp $params = "[?][$vura]*"; // Additional parameters (for GET) $str = eregi_replace("$protocol$server(/$path($name)?)?($params)?", "<a href=\"\\0\">\\0</a>", $str); // URL into links $str = str_replace("^^sentinal^^", '', $str); return str_replace($search, $replace, $str); } -hdn ---- Manual Page -- http://www.php.net/manual/en/function.ereg-replace.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+36370 Delete -- http://master.php.net/manage/user-notes.php?action=delete+36370&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+36370&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#57973) next »