JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
add: enc_linkify() (add <a> tags to urls in text)
authorJason Woofenden <jason@jasonwoof.com>
Wed, 7 Sep 2011 17:19:14 +0000 (13:19 -0400)
committerJason Woofenden <jason@jasonwoof.com>
Wed, 7 Sep 2011 17:27:54 +0000 (13:27 -0400)
encode.php

index 4ef72ae..3e9ee0c 100644 (file)
@@ -432,3 +432,21 @@ function enc_s($str) {
 
        return 's';
 }
+
+# turn http/ftp (s) urls into html links (and encode everything for html)
+# does not encode without protocol (eg "www.foo.com")
+# does not linkify email addresses
+function enc_linkify($str) {
+       $ret = '';
+       $even = true;
+       $pieces = preg_split("/((?:ht|f)tps?:\/\/[^ \,\"\n\r\t<]+)/is", $str, null, PREG_SPLIT_DELIM_CAPTURE);
+       foreach($pieces as $piece) {
+               if($even) {
+                       $ret .= enc_html($piece);
+               } else {
+                       $ret .= '<a href="' . enc_attr($piece) . '">' . enc_html($piece) . '</a>';
+               }
+               $even = !$even;
+       }
+       return $ret;
+}