Added generic method for generating anchor tags
authorMarcel Werk <burntime@woltlab.com>
Wed, 6 Feb 2013 00:47:28 +0000 (01:47 +0100)
committerMarcel Werk <burntime@woltlab.com>
Wed, 6 Feb 2013 00:47:28 +0000 (01:47 +0100)
wcfsetup/install/files/lib/util/StringUtil.class.php

index 090610894f4ad17e7b82d3bb91c807102927bf1c..3b4c0611cc4ec81b7c8197122fd7fc2b3d37449d 100644 (file)
@@ -1,5 +1,6 @@
 <?php
 namespace wcf\util;
+use wcf\system\application\ApplicationHandler;
 use wcf\system\WCF;
 
 /**
@@ -638,6 +639,32 @@ final class StringUtil {
                return $truncatedString;
        }
        
+       /**
+        * Generates an anchor tag from given URL.
+        *  
+        * @param       string          $url
+        * @param       string          $title
+        * @return      string          anchor tag
+        */
+       public static function getAnchorTag($url, $title = '') {
+               $external = true;
+               if (ApplicationHandler::getInstance()->isInternalURL($url)) {
+                       $external = false;
+               }
+               
+               // cut visible url
+               if (empty($title)) {
+                       // use URL and remove protocol and www subdomain 
+                       $title = preg_replace('~^(?:https?|ftps?)://(?:www\.)?~i', '', $url);
+                       
+                       if (self::length($title) > 60) {
+                               $title = self::substring($title, 0, 30) . self::HELLIP . self::substring($title, -25);
+                       }
+               }
+               
+               return '<a href="'.self::encodeHTML($url).'"'.($external ? (' class="externalURL"'.(EXTERNAL_LINK_REL_NOFOLLOW ? ' rel="nofollow"' : '').(EXTERNAL_LINK_TARGET_BLANK ? ' target="_blank"' : '')) : '').'>'.self::encodeHTML($title).'</a>';
+       }
+       
        /**
         * Splits given string into smaller chunks.
         *