note 43771 deleted from function.preg-match-all by colder

From: Date: Mon, 16 Apr 2007 16:54:42 +0000
Subject: note 43771 deleted from function.preg-match-all by colder
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-125454@lists.php.net to get a copy of this message
Note Submitter: hex6ng at yahoo dot com ---- This is a much more efficient version of the same function posted in ereg_replace() discussion by hdn, who is the same person as hex6ng. I didn't include activating urls without http:// protocol identifier because there are many xxx.xxx patterns that are not urls. function html_activate_urls($str) { // lift all links, images and image maps $url_tags = array ( "'<a[^>]*>.*?</a>'si", "'<map[^>]*>.*?</map>'si", "'<script[^>]*>.*?</script>'si", "'<style[^>]*>.*?</style>'si", "'<[^>]+>'si" ); foreach($url_tags as $url_tag) { preg_match_all($url_tag, $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"); $str = preg_replace($sentinals, "<marker>\\0", $str); // URL into links $str = preg_replace( "|\w{3,10}://[\w\.\-_]+(:\d+)?[^\s\"\'<>\(\)\{\}]*|", "<a href=\"\\0\">\\0</a>", $str ); $str = str_replace("<marker>", '', $str); return str_replace($search, $replace, $str); } -hdn

« previous php.notes (#125454) next »