make_clickable(): improve regular expression of converting URL to ignore the beginnin...
[acontent.git] / docs / include / lib / output.inc.php
index cb2d0d5..6aa0d81 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 /************************************************************************/
-/* AContent                                                         */
+/* AContent                                                             */
 /************************************************************************/
-/* Copyright (c) 2009                                                   */
-/* Adaptive Technology Resource Centre / University of Toronto          */
+/* Copyright (c) 2010                                                   */
+/* Inclusive Design Institute                                           */
 /*                                                                      */
 /* This program is free software. You can redistribute it and/or        */
 /* modify it under the terms of the GNU General Public License          */
@@ -669,12 +669,23 @@ function embed_media($text) {
 function make_clickable($text) {
        $text = embed_media($text);
 
-       $text = preg_replace("/([\s])(http[s]?):\/\/([\^\s\<]*)([a-zA-Z0-9\#\?\/\&\=])/i", 
-                            "\\1<a href=\"\\2://\\3\\4\">\\3\\4</a>", $text);
+       // The next 3 preg_replace convert plain text URL to clickable URL.
+       // limited conversion. It doesn't cover the case when the stuff in front of the URL is not a word. For example:
+       // <p>http://google.ca</p>
+       // "http://google.ca"  
+       // 1. remove the spaces in [media] tag, otherwise, the next line converts URL inside [media] into <a> tag
+       $text = preg_replace("/(\[media\])([\s]*)(.*)(\[\/media\])/", '$1$3$4', $text);
+       $text = preg_replace("/(\[media\])(.*)([\s]*)(\[\/media\])/U", '$1$2$4', $text);
+       // 2. convert URL
+       $text = preg_replace('/(^|[\n ])([\w]*?[\"]*)((?<!(\[media\]))http(s)?:\/\/[\w]+[^ \,\"\n\r\t\)<]*)/is', 
+                            '$1$2<a href="$3">$3</a>', $text);
        
-       $text = preg_replace('/([_a-zA-Z0-9\-]+(\.[_a-zA-Z0-9\-]+)*'.
-                                               '\@'.'[_a-zA-Z0-9\-]+(\.[_a-zA-Z0-9\-]+)*'.'(\.[a-zA-Z]{1,6})+)/i',
-                                               "<a href=\"mailto:\\1\">\\1</a>",
+       // convert email address to clickable URL that pops up "send email" interface with the address filled in
+       $text = preg_replace('/(?|<a href="mailto[\s]*:[\s]*([_a-zA-Z0-9\-]+(\.[_a-zA-Z0-9\-]+)*'.'\@'
+                            .'[_a-zA-Z0-9\-]+(\.[_a-zA-Z0-9\-]+)*'.'(\.[a-zA-Z]{1,6})+)">(.*)<\/a>'
+                            .'|((((([_a-zA-Z0-9\-]+(\.[_a-zA-Z0-9\-]+)*'.'\@'
+                            .'[_a-zA-Z0-9\-]+(\.[_a-zA-Z0-9\-]+)*'.'(\.[a-zA-Z]{1,6})+))))))/i',
+                                               "<a href=\"mailto:\\1\">\\5</a>",
                                                $text);
        
        return $text;