Posts Tagged ‘ url

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.

<?php
   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 = "www.meineurl.de oder http://www.meineurl.de oder meine@email.de";
  $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!