OSDN Git Service

Add "prepareBody_cb" function for coditional branch of "createLinkCode" function.
authorsakamocchi <sakamocchi@1ca29b6e-896d-4ea0-84a5-967f57386b96>
Mon, 21 Mar 2011 02:14:01 +0000 (02:14 +0000)
committersakamocchi <sakamocchi@1ca29b6e-896d-4ea0-84a5-967f57386b96>
Mon, 21 Mar 2011 02:14:01 +0000 (02:14 +0000)
git-svn-id: https://svn.sourceforge.jp/svnroot/nucleus-jp/nucleus-jp/trunk@1172 1ca29b6e-896d-4ea0-84a5-967f57386b96

utf8/nucleus/libs/COMMENT.php

index ffaa925..13c470b 100755 (executable)
@@ -95,20 +95,16 @@ class COMMENT {
 
                // create hyperlinks for http:// addresses
                // there's a testcase for this in /build/testcases/urllinking.txt
-               $replaceFrom = array(
-                       '/([^:\/\/\w]|^)((https:\/\/)([\w\.-]+)([\/\w+\.~%&?@=_:;#,-]+))/ie',
-                       '/([^:\/\/\w]|^)((http:\/\/|www\.)([\w\.-]+)([\/\w+\.~%&?@=_:;#,-]+))/ie',
-                       '/([^:\/\/\w]|^)((ftp:\/\/|ftp\.)([\w\.-]+)([\/\w+\.~%&?@=_:;#,-]+))/ie',
-                       '/([^:\/\/\w]|^)(mailto:(([a-zA-Z\@\%\.\-\+_])+))/ie'
+\r
+               $replace_from = array(\r
+                       '/([^:\/\/\w]|^)((https:\/\/)([\w\.-]+)([\/\w+\.~%&?@=_:;#,-]+))/i',\r
+                       '/([^:\/\/\w]|^)((http:\/\/|www\.)([\w\.-]+)([\/\w+\.~%&?@=_:;#,-]+))/i',\r
+                       '/([^:\/\/\w]|^)((ftp:\/\/|ftp\.)([\w\.-]+)([\/\w+\.~%&?@=_:;#,-]+))/i',\r
+                       '/([^:\/\/\w]|^)(mailto:(([a-zA-Z\@\%\.\-\+_])+))/i'\r
                );
-               $replaceTo = array(
-                       'COMMENT::createLinkCode("\\1", "\\2","https")',
-                       'COMMENT::createLinkCode("\\1", "\\2","http")',
-                       'COMMENT::createLinkCode("\\1", "\\2","ftp")',
-                       'COMMENT::createLinkCode("\\1", "\\3","mailto")'
-               );
-               $body = preg_replace($replaceFrom, $replaceTo, $body);
-               
+
+               $body = preg_replace_callback($replace_from, array('self', 'prepareBody_cb'), $body);
+
                return $body;
        }
 
@@ -169,5 +165,39 @@ class COMMENT {
 
                return $pre . '<a href="' . $linkedUrl . '" rel="nofollow">' . shorten($displayedUrl,30,'...') . '</a>' . $post;
        }
+
+\r
+       /**\r
+        * This method is a callback for creating link codes\r
+        * @param array $match\r
+        * @return string\r
+        */\r
+       function prepareBody_cb($match)\r
+       {\r
+               if ( !preg_match('/^[a-z]+/i', $match[2], $protocol) )\r
+               {\r
+                       return $match[0];\r
+               }\r
+\r
+               switch( strtolower($protocol[0]) )\r
+               {\r
+                       case 'https':\r
+                               return self::createLinkCode($match[1], $match[2], 'https');\r
+                       break;\r
+\r
+                       case 'ftp':\r
+                               return self::createLinkCode($match[1], $match[2], 'ftp');\r
+                       break;\r
+\r
+                       case 'mailto':\r
+                               return self::createLinkCode($match[1], $match[3], 'mailto');\r
+                       break;\r
+\r
+                       default:\r
+                               return self::createLinkCode($match[1], $match[2], 'http');\r
+                       break;\r
+               }\r
+       }
+
 }
 ?>
\ No newline at end of file