note 43771 added to function.preg-match-all
| From: | hex6ng at yahoo dot com | Date: | Fri, 02 Jul 2004 19:04:22 +0000 |
| Subject: | note 43771 added to function.preg-match-all | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-72344@lists.php.net to get a copy of this message | ||
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
----
Manual Page -- http://www.php.net/manual/en/function.preg-match-all.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+43771
Delete -- http://master.php.net/manage/user-notes.php?action=delete+43771&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+43771&report=yes
Search -- http://master.php.net/manage/user-notes.php