Archive for the ‘ PHP Snippets ’ Category

Automatically create HTML-links for urls

There’s a little php code snippet as example for replacing pure text-url into a clickable HTML-link. The presentation of this php snippet is just used as a test of the code highlighting I want to use on this page.

   function AutoVerlinkung ($msg) {
     // http
     $msg = str_replace("http://www.","www.",$msg);
     $msg = str_replace("www.","http://www.",$msg);
     $msg = preg_replace("/([\w]+:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/i","<A TARGET=\"_blank\" HREF=\"$1\">$1</A>", $msg);

     // mail
      $msg = preg_replace("/([\w-?&;#~=\.\/]+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?))/i","<A HREF=\"mailto:$1\">$1</A>",$msg);
      return $msg;

   $demo = " oder oder";
  $demoVerlinkt = AutoVerlinkung($demo);
  echo "<p>aus: <b>".$demo."</b></p>";
  echo "<p>wird: <b>".$demoVerlinkt."</b></p>";

The seen snippet belongs to a unknown, brilliant developer. I hope not to hurt any rights by showing this bit of code.

Many thanks in advance for your appreciation!