phpbar.de logo

Mailinglisten-Archive

[php] Re: url filtern

[php] Re: url filtern

Martin Klässner php_(at)_phpcenter.de
Tue, 3 Apr 2001 16:56:58 +0200


Hi,

folgende Funktion parsed alle Links.

cu Martin
http://www.webmaster-foren.de

-- schnipp --
<?
function auto_link($str)


  # don't use target if tail is follow
  $regex[file] =
"gz|tgz|tar|gzip|zip|rar|mpeg|mpg|exe|com|rpm|dep|rm|ram|asf|ace|viv";

  # define URL
  $regex[http] =
"(http|https|ftp|telnet|news):\/\/([a-z0-9_\-]+\.[][a-z0-9:;&#_(at)_=_~%\?\/\.\,\
+\-]+)";

  # define Link ohne http://
  #$regex[link] = "(www)(\.[][a-z0-9:;&#_(at)_=_~%\?\/\.\,\+\-]+)";

  # define E-mail address
  $regex[mail] = "([a-z0-9_\-]+)_(at)_([a-z0-9_\-]+\.[a-z0-9\-\._\-]+)";

  # don't use target
  $regex[notarget] =
"(http|https|ftp|news):\/\/([a-z0-9_\-]+\.[][a-z0-9:;&#_(at)_=_~%\?\/\.\,\+\-]+)\
/[][a-z0-9:#_(at)__~%\/\.\,\+\-]+\.($regex[file])";

  # If use "wrap=hard" option in TEXTAREA tag,
  # connected 2 lines that devided 2 lines in a link
  $str = eregi_replace("<([^<>\n]+)\n([^\n<>]+)>", "<\\1 \\2>", $str);

  # replaced special char and delete target
  $str = eregi_replace("&(quot|gt|lt)","!\\1",$str);
  $str = eregi_replace("([ ]+)target=[\"'_a-z,A-Z]+","", $str);
  $str = eregi_replace("([ ]+)on([a-z]+)=[\"'_a-z,A-Z\?\.\-_\/()]+","",
$str);

  # protected link when use html link code
  # protected URL
  $str = eregi_replace("<a([ ]+)href=([\"']*)($regex[http])([\"']*)>","<a
class=\"sm\" href=\"\\4_orig://\\5\" target=\"_blank\">", $str);
  # protected E-mail
  $str =
regi_replace("<a([ ]+)href=([\"']*)mailto:($regex[mail])([\"']*)>","<a
class=\"sm\" href=\"mailto:\\4#-#\\5\">", $str);
  # protected Image link
  $str = eregi_replace("<img([ ]*)src=([\"']*)($regex[http])([\"']*)","<img
src=\"\\4_orig://\\5\"",$str);
  # protected flash link
  $str =
regi_replace("<embed([ ]*)src=([\"']*)($regex[http])([\"']*)","<embed
src=\"\\4_orig://\\5\"",$str);

  # auto linked url and email address that unlinked
  $str = eregi_replace("($regex[http])","<a class=\"sm\" href=\"\\1\"
target=\"_blank\">\\1</a>", $str);
#  $str = eregi_replace("($regex[link])","<a class=\"sm\"
href=\"http:\/\\1\" target=\"_blank\">\\1</a>", $str);
  $str = eregi_replace("($regex[mail])","<a class=\"sm\"
href=\"mailto:\\1\">\\1</a>", $str);

  # restored code that replaced for protection
  $str = eregi_replace("!(quot|gt|lt)","&\\1",$str);
  $str = eregi_replace("(http|https|ftp|telnet|news)_orig","\\1", $str);
  $str = eregi_replace("#-#","_(at)_",$str);

  # delete multiple link
  $str = eregi_replace("(<a href=([\"']*)($regex[http])([\"']*)+([^>]*)>)+<a
href=([\"']*)($regex[http])([\"']*)+([^>]*)>","\\1", $str);
  $str = eregi_replace("(<a href=([\"']*)mailto:($regex[mail])([\"']*)>)+<a
href=([\"']*)mailto:($regex[mail])([\"']*)>","\\1", $str);
  $str = str_replace("</a></a>","</a>",$str);

  # if url is file, delete target
  $str = eregi_replace("(<a class=\"sm\" href=\"$regex[notarget]\")+
target=\"_blank\"","\\1",$str);

  return $str;
}
?>

-- schnapp --



php::bar PHP Wiki   -   Listenarchive