Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

linkify.inc.php

Go to the documentation of this file.
00001 <?
00009 class Linkify
00010 {
00017     public static function html2bbcode($text)
00018     { 
00019         $text = preg_replace("/<img src='(.*)'>/i", "[img]$1[/img]", $text);
00020         $text = preg_replace("/<img src=\"(.*)\">/i", "[img]$1[/img]", $text);
00021 
00022         $text = preg_replace("/<br>/i", "\n", $text);
00023         $text = preg_replace("/<hr>/i", "[hr]", $text);
00024 
00025         $text = preg_replace("/<b>/i", "[b]", $text);
00026         $text = preg_replace("/<\/b>/i", "[/b]", $text);
00027 
00028         $text = preg_replace("/<u>/i", "[u]", $text);
00029         $text = preg_replace("/<\/u>/i", "[/u]", $text);
00030 
00031         $text = preg_replace("/<i>/i", "[i]", $text);
00032         $text = preg_replace("/<\/i>/i", "[/i]", $text);
00033         
00034         $text = preg_replace("/<blink>/i", "[blink]", $text);
00035         $text = preg_replace("/<\/blink>/i", "[/blink]", $text);
00036 
00037         $text = preg_replace("/(^|[^\"'=\]])(http|ftp|https):\/\/([\w.\-_:]*)(\/[\w%=+\-&?!@#$^&*~{}|,.\/]*)?/i",   "$1[url=$2://$3$4]$3[/url]", $text);
00038         $text = preg_replace("/(^|[^\/\]])(www\.[\w%=+\-&?!@#$^&*~{}|,.\/:]*)/i", "$1[url=http://$2]$2[/url]", $text);
00039         $text = preg_replace("/<a href='(.*)?'>/Usi", "[url=$1]", $text);
00040         $text = preg_replace("/<a href=\"(.*)?\">/Usi", "[url=$1]", $text);
00041         $text = preg_replace("/<\/a>/i", "[/url]", $text);
00042 
00043         $text = preg_replace("/<pre>(.*)?<\/pre>/Usi", "[code]$1[/code]", $text);
00044         $text = preg_replace("/<marquee(.*)?>(.*)?<\/marquee>/Usi", "[marquee$1]$2[/marquee]", $text);
00045 
00046         $text = str_replace("<ol>", "[list=1]", $text);
00047         $text = str_replace("</ol>", "[/list]", $text);
00048         $text = str_replace("<ul>", "[list]", $text);
00049         $text = str_replace("</ul>", "[/list]", $text);
00050         $text = str_replace("<li>", "[*]", $text);      
00051         $text = str_replace("</li>", "", $text);        
00052 
00053         return $text;
00054     } 
00055 
00065     public static function bbcode($text)
00066     {
00067         //trim it...
00068         $text = stripslashes(trim($text));
00069         
00070         //parse html2bbcode, then get rid of all html, then parse bbcode back2 html
00071         $text = self::html2bbcode($text);
00072         $text = htmlentities($text);
00073 
00074         //add hearts...
00075         $text = str_replace( "&lt;/3", "[img]lib/img/smileys/broken-heart.gif[/img]", $text );
00076         $text = str_replace( "&lt;3", "[img]lib/img/smileys/heart.gif[/img]", $text );
00077         $text = str_replace( "::bling::", "[color=green][b]\$\$\$\$[/b][/color]", $text );
00078         $text = str_replace( "&lt;*&gt;", "[img]lib/img/smileys/pink-star.gif[/img]", $text );
00079         $text = str_replace( "&lt;jesus&gt;", "[img]lib/img/smileys/jesus.gif[/img]", $text );
00080         $text = str_replace( "&lt;satan&gt;", "[img]lib/img/smileys/devil.gif[/img]", $text );
00081         $text = str_replace( "&lt;elvis&gt;", "[img]lib/img/smileys/elvis.gif[/img]", $text );
00082         $text = str_replace( "&lt;hoeken&gt;", "[img]lib/img/smileys/hoeken.gif[/img]", $text );
00083         $text = str_replace( "&lt;airplane&gt;", "[marquee][img]lib/img/smileys/airplane.jpg[/img]VROOOM![/marquee]", $text );
00084         $text = str_replace( "&lt;fish&gt;", "[img]lib/img/smileys/fish.gif[/img]", $text );
00085         $text = str_replace( "&lt;cow&gt;", "[img]lib/img/smileys/cow.jpg[/img]", $text );
00086         $text = str_replace( "&lt;zebra&gt;", "[img]lib/img/smileys/zebra.jpg[/img]", $text );
00087         $text = str_replace( "&lt;kravin&gt;", "[img]lib/img/smileys/kravin.gif[/img]", $text );
00088         $text = str_replace( "&lt;turtle&gt;", "[img]lib/img/smileys/turtle.gif[/img]", $text );
00089         $text = str_replace( "&lt;potato&gt;", "[img]lib/img/smileys/potato.gif[/img]", $text );
00090         $text = str_replace( "&lt;monkey&gt;", "[img]lib/img/smileys/monkey.gif[/img]", $text );
00091         $text = str_replace( "&lt;sun&gt;", "[img]lib/img/smileys/sun.gif[/img]", $text );
00092         $text = str_replace( "&lt;moon&gt;", "[img]lib/img/smileys/moon.gif[/img]", $text );
00093         $text = str_replace( "&lt;eclipse&gt;", "[img]lib/img/smileys/eclipse.gif[/img]", $text );
00094         $text = str_replace( "&lt;beer&gt;", "[img]lib/img/smileys/beer.gif[/img]", $text );
00095         $text = str_replace( "&lt;dino&gt;", "[img]lib/img/smileys/dino.gif[/img]", $text );
00096         $text = str_replace( "&lt;chicken&gt;", "[img]lib/img/smileys/chicken.gif[/img]", $text );
00097         $text = str_replace( "&lt;robot&gt;", "[img]lib/img/smileys/robot.gif[/img]", $text );
00098         $text = str_replace( "&lt;rainbowbrite&gt;", "[img]lib/img/smileys/rainbow.gif[/img]", $text );
00099         
00100         //now convert to bbcode...
00101         $text = self::bbcode2html($text);   
00102         
00103         //add breaks...
00104         $text = str_replace( "\n", "<br>\n", $text );
00105         
00106         //fix the preformatted text
00107         $text = preg_replace_callback("/<pre>(.*)?<\/pre>/Usi", 
00108             create_function(
00109                 '$matches',
00110                 'return Linkify::formatPre(str_replace("&quot;", "\"", $matches[1]));'
00111             ),
00112             $text
00113         );
00114     
00115         return $text;
00116     }
00117     
00124     public static function bbcode2html($text)
00125     { 
00126         global $me;
00127         
00128         //get images first... dont move this
00129         $text = preg_replace("/\[img\](.*)?\[\/img\]/Ui", "<img src=\"$1\" border=\"0\">", $text);
00130         
00131         //next get bold and italic tags
00132         $text = preg_replace("/\[b\]/i", "<b>", $text);
00133         $text = preg_replace("/\[\/b\]/i", "</b>", $text);
00134         $text = preg_replace("/\[u\]/i", "<u>", $text);
00135         $text = preg_replace("/\[\/u\]/i", "</u>", $text);
00136         $text = preg_replace("/\[i\]/i", "<i>", $text);
00137         $text = preg_replace("/\[\/i\]/i", "</i>", $text);
00138         
00139         //do we display it??
00140         $text = preg_replace("/\[blink\]/i", "<blink>", $text);
00141         $text = preg_replace("/\[\/blink\]/i", "</blink>", $text);
00142         
00143         //now do urls and emails
00144         $text = preg_replace("/\[url\](.*)?\[\/url\]/Ui", '<a href="$1">$1</a>', $text);
00145         $text = preg_replace("/\[url=(.*)?\]/Ui", "<a href=\"$1\">", $text);
00146         $text = preg_replace("/\[\/url\]/", "</a>", $text);
00147         
00148         //do we display it??
00149         $text = preg_replace("/\[email\](.*)?(\[\/email\])/Ui", "<a href=\"mailto:$1\">$1</a>", $text);
00150             
00151         //first we get teh list entries
00152         $text = preg_replace("/\[\*\](.*)/", "<li>$1</li>", $text); 
00153         
00154         //strip newlines from the tags that dont want it... code, lists, etc.
00155         $text = preg_replace_callback("/\[list[=a1]*\](.*)?\[\/list\]/Usi",
00156             create_function(
00157                 '$matches',
00158                 'return Linkify::stripNewlines(str_replace("&quot;", "\"", $matches[0]));'
00159             ),
00160             $text
00161         );
00162         
00163         $text = preg_replace("/\[list\](.*)?\[\/list\]/Ui", "<ul>$1</ul>", $text);
00164         $text = preg_replace("/\[list=a\](.*)?\[\/list\]/Ui", "<ol type=\"a\">$1</ol>", $text);
00165         $text = preg_replace("/\[list=1\](.*)?\[\/list\]/Ui", "<ol type=\"1\">$1</ol>", $text);
00166 
00167         //catch our code!!!
00168         $text = preg_replace("/\[code\](.*)?\[\/code\]/Usi",
00169             '<table class="code" cellpadding="3" cellspacing="0"><tr>' . 
00170             '<td><pre>$1</pre></td></tr></table>', $text);
00171         
00172         //how bout our quotes?
00173         if(substr_count(strtolower($text), "[quote") == substr_count(strtolower($text), "[/quote]"))
00174         {
00175             $text = str_replace('[quote]', '<table class="quote" cellpadding="3" cellspacing="0"><tr><td><i>', $text);
00176             $text = preg_replace('/\[quote=&quot;(.*)&quot;\]/Usi', '<table class="quote" cellpadding="3" cellspacing="0"><tr>' . 
00177                 "<td class=\"dk_row\"><font class=\"med-small\"><b>$1 said:</b></font></td></tr><tr><td><i>", $text);
00178             $text = str_replace('[/quote]', '</i></td></tr></table>', $text);
00179         }
00180         
00181         //get our text formatitng and shit
00182         $text = preg_replace("/\[color=(.*)?\]/Usi", "<span style=\"color:$1\">", $text);
00183         $text = preg_replace("/\[\/color\]/i", "</span>", $text);
00184         
00185         //get our text formatitng and shit
00186         $text = preg_replace("/\[bgcolor=(.*)?\]/Usi", "<span style=\"background-color:$1\">", $text);
00187         $text = preg_replace("/\[\/bgcolor\]/i", "</span>", $text);
00188         
00189         $text = preg_replace("/\[strike\]/i", "<span style=\"text-decoration: line-through;\">", $text);
00190         $text = preg_replace("/\[\/strike\]/i", "</span>", $text);
00191         
00192         $text = preg_replace("/\[size=(([0-2]{0,1})[0-9])?\]/Usi", "<span style=\"font-size:$1px\">", $text);
00193         $text = preg_replace("/\[\/size\]/i", "</span>", $text);
00194         
00195         $text = preg_replace("/\[font=(.*)?\]/Usi", "<span style=\"font-family:$1\">", $text);
00196         $text = preg_replace("/\[\/font\]/i", "</span>", $text);
00197         
00198         //how bout alignment??
00199         $text = preg_replace("/\[align=(.*)?\]/Usi", "<div style=\"text-align:$1\">", $text);
00200         $text = preg_replace("/\[\/align\]/i", "</div>", $text);
00201             
00202         //who doesnt like marquee?
00203         $text = preg_replace_callback(
00204             '/\[marquee(.*)?\](.*)?\[\/marquee\]/Usi',
00205             create_function(
00206                 '$matches',
00207                 'return "<marquee" . str_replace("&quot;", \'"\', $matches[1]) . ">$matches[2]</marquee>";'
00208             ),
00209             $text
00210         );
00211         $text = preg_replace("/\[scroll\]/i", "<marquee>", $text);
00212         $text = preg_replace("/\[\/scroll\]/i", "</marquee>", $text);
00213             
00214         $text = preg_replace_callback("/\[caps\](.*)?\[\/caps\]/Usi",
00215             create_function(
00216                 '$matches',
00217                 'return Linkify::capsText(str_replace("&quot;", "\"", $matches[1]));'
00218             ),
00219             $text
00220         );
00221         
00222         $text = preg_replace_callback("/\[haxor\](.*)?\[\/haxor\]/Usi",
00223             create_function(
00224                 '$matches',
00225                 'return Linkify::haxorText(str_replace("&quot;", "\"", $matches[1]));'
00226             ),
00227             $text
00228         );
00229         
00230         $text = preg_replace_callback("/\[rainbow\](.*)?\[\/rainbow\]/Usi",
00231             create_function(
00232                 '$matches',
00233                 'return Linkify::rainbowText(str_replace("&quot;", "\"", $matches[1]));'
00234             ),
00235             $text
00236         );
00237         
00238         $text = preg_replace_callback("/\[roller\](.*)?\[\/roller\]/Usi",
00239             create_function(
00240                 '$matches',
00241                 'return Linkify::rollerText(str_replace("&quot;", "\"", $matches[1]));'
00242             ),
00243             $text
00244         );
00245 
00246         $text = str_replace("[hr]", "<hr>", $text);
00247 
00248         return $text;
00249     }  
00250 
00257     public static function capsText($text)
00258     { 
00259         $fs=0;
00260         $direction="right";
00261         $rtext = "";
00262         $stop = false;
00263 
00264         //loop thru and rollerize it.
00265         for ($i=0; $i<strlen($text); $i++)
00266         {
00267             if($text[$i] == "<" || $text[$i] == "[")
00268                 $stop = true;
00269         
00270             if(!$stop && $text[$i] != " ")
00271             {
00272                 $l = strtolower($text[$i]);
00273                 
00274                 if($l >= 'a' && $l <= 'z')
00275                 {
00276                     //randomly convert to upper
00277                     if(mt_rand(0,1))
00278                         $l = strtoupper($l);
00279                 }
00280                 else if($l == '&')
00281                 {
00282                     $end = strpos($text, ";", $i);
00283                     if(!($end === false))
00284                     {
00285                         $l = substr($text, $i, $end-$i+1);
00286                         $i = $end;
00287                     }
00288                 }
00289                     
00290                 $rtext .= $l;
00291             }
00292             else
00293                 $rtext .= $text[$i];
00294             
00295             if($text[$i] == ">" || $text[$i] == "]")
00296                 $stop = false;
00297         }
00298 
00299         return $rtext;
00300      }
00301     
00308     public static function rollerText($text)
00309     {
00310         $fs=0;
00311         $direction="right";
00312         $rtext = "";
00313         $stop = false;
00314 
00315         $sizes = array( 11, 14, 18, 22, 25, 28 );
00316 
00317         //loop thru and rollerize it.
00318         for ($i=0; $i<strlen($text); $i++)
00319         {
00320             if($text[$i] == "<" || $text[$i] == "[")
00321                 $stop = true;
00322         
00323             if(!$stop && $text[$i] != " ")
00324             {
00325                 $l = $text[$i];
00326                 
00327                 if($l == "&")
00328                 {
00329                     $end = strpos($text, ";", $i);
00330                     if(!($end === false))
00331                     {
00332                         $l = substr($text, $i, $end-$i+1);
00333                         $i = $end;
00334                     }
00335                 }
00336                 
00337                 $rtext .= "<span style=\"font-size:" . $sizes[$fs] . "px\">$l</span>";
00338 
00339                 if ($fs<count($sizes)-1 && $direction=="right")
00340                     $fs++;
00341                 else if ($fs==count($sizes)-1)
00342                 { 
00343                     $direction="left";
00344                     $fs--;
00345                 }
00346                 else if ($fs==0)
00347                 {
00348                     $direction="right";
00349                     $fs++;
00350                 } 
00351                 else if ($fs>0 && $direction=="left")
00352                     $fs--;
00353             }
00354             else
00355                 $rtext .= $text[$i];
00356             
00357             if($text[$i] == ">" || $text[$i] == "]")
00358                 $stop = false;
00359         }
00360 
00361         return $rtext;
00362      }
00363     
00370     public static function rainbowText($text)
00371     {
00372         $colors = array( "#ff0000", "#ff5000", "#ffa100", "#fff100", "#bbff00", "#6bff00", "#1aff00", "#00ff35", "#00ff86", "#00ffd6", "#00d6ff", "#0086ff", "#3377ff", "#4444ff", "#6b33ff", "#bb00ff", "#ff00f1", "#ff00a1", "#ff0050", "#ff0000" );  
00373         $fs=0;
00374         $direction="right";
00375         $rtext = "";
00376         $stop = false;
00377 
00378         //loop thru and rollerize it.
00379         for ($i=0; $i<strlen($text); $i++)
00380         {
00381             if($text[$i] == "<" || $text[$i] == "[")
00382                 $stop = true;
00383         
00384             if(!$stop && $text[$i] != " ")
00385             {
00386                 $l = $text[$i];
00387                 
00388                 if($l == "&")
00389                 {
00390                     $end = strpos($text, ";", $i);
00391                     if(!($end === false))
00392                     {
00393                         $l = substr($text, $i, $end-$i+1);
00394                         $i = $end;
00395                     }
00396                 }
00397                 
00398                 $rtext .= "<span style=\"color:" . $colors[$fs] . ";\">$l</span>";
00399 
00400                 if ($fs<count($colors)-1 && $direction=="right")
00401                     $fs++;
00402                 else if ($fs==count($colors)-1)
00403                 {
00404                     $direction="left";
00405                     $fs--;
00406                 }
00407                 else if ($fs==0)
00408                 {
00409                     $direction="right";
00410                     $fs++;
00411                 } 
00412                 else if ($fs>0 && $direction=="left")
00413                     $fs--;
00414             }
00415             else
00416                 $rtext .= $text[$i];
00417             
00418             if($text[$i] == ">" || $text[$i] == "]")
00419                 $stop = false;
00420         }
00421 
00422         $rtext = "<span style=\"background-color: #000000;\">$rtext</span>";
00423         return $rtext;
00424     }
00425     
00432     public static function haxorText($text)
00433     {
00434         $text = strtolower($text);
00435     
00436         //loop thru and rollerize it.
00437         for ($i=0; $i<strlen($text); $i++)
00438         {
00439             if($text[$i] == "<" || $text[$i] == "[")
00440                 $stop = true;
00441         
00442             if(!$stop)
00443             {
00444                 $l = $text[$i];
00445                 if($l == 'a')
00446                     $r = '@';
00447                 else if($l == 'b')
00448                     $r = '8';
00449                 else if($l == 'c')
00450                     $r = '&copy;';
00451                 else if($l == 'd')
00452                     $r = '&ETH;';
00453                 else if($l == "e")
00454                     $r = '&euro;';
00455                 else if($l == 'h')
00456                     $r = '<b>|-|</b>';
00457                 else if($l == 'i')
00458                     $r = '&Icirc;';
00459                 else if($l == 'k')
00460                     $r = '<b>|</b>&lt;';
00461                 else if($l == 'l')
00462                     $r = '&pound;';
00463                 else if($l == 'm')
00464                     $r = '<b>|\/|</b>';
00465                 else if($l == 'n')
00466                     $r = '&ntilde;';
00467                 else if($l == 'o')
00468                     $r = '&Oslash;';
00469                 else if($l == 'p')
00470                     $r = '&para;';
00471                 else if($l == 'r')
00472                     $r = '&reg;';
00473                 else if($l == 's')
00474                     $r = '$';
00475                 else if($l == 't')
00476                     $r = '<b>&dagger;</b>';
00477                 else if($l == 'u')
00478                     $r = '&uuml;';
00479                 else if($l == 'v')
00480                     $r = '<b>\/</b>';
00481                 else if($l == 'w')
00482                     $r = '<b>\/\/</b>';
00483                 else if($l == 'x')
00484                     $r = '&times;';
00485                 else if($l == 'y')
00486                     $r = '&yen;';
00487                 else if($l == '?')
00488                     $r = '&iquest;';
00489                 else
00490                     $r = $l;
00491             
00492                 $rtext .= $r;
00493             }
00494             else
00495                 $rtext .= $text[$i];
00496             
00497             if($text[$i] == ">" || $text[$i] == "]")
00498                 $stop = false;
00499         }
00500 
00501         return $rtext;
00502     }
00503     
00510     public static function stripNewlines($text)
00511     {
00512         return str_replace("\n", "", $text);
00513     }
00514 
00521     public static function formatPre($text)
00522     {
00523         return "<pre>" . str_replace("<br>", "", $text) . "</pre>";
00524     }
00525     
00533     public static function email($email, $display = null)
00534     {
00535         $emailSplit = explode("@", $email);
00536 
00537         if (count($emailSplit) == 2)
00538         {
00539             $result = "<script type=\"text/javascript\"><!--\n";
00540             $result .= "document.write('<a href=\"mailto:' + (['$emailSplit[0]', '$emailSplit[1]'].join('@')) + '\">' + ";
00541 
00542             if ($display)
00543                 $result .= "'" . str_replace("\n", "\\n", str_replace("'", "\\'", $display)) . "'";
00544             else
00545                 $result .= "(['$emailSplit[0]', '$emailSplit[1]'].join('@'))";
00546 
00547             $result .= " + '<\/a>');";
00548             $result .= "//--></script>";
00549 
00550             $textEmail = str_replace(".", " (dot) ", $email);
00551             $textEmail = str_replace("@", " (at) ", $textEmail);
00552 
00553             $result .= "<ins><noscript><div>";
00554             if ($display)
00555                 $result .= "$display ($textEmail)";
00556             else
00557                 $result .= $textEmail;
00558             $result .= "</div></noscript></ins>";
00559 
00560             return $result;
00561         }
00562 
00563         return '';
00564     }
00565 }
00566 ?>

Generated on Fri Oct 27 12:26:40 2006 for BaseJumper by doxygen 1.3.9.1