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

util.inc.php

Go to the documentation of this file.
00001 <?php
00010 class Util
00011 {
00017     public static function redirect($url)
00018     {
00019         header("Location: $url");
00020         exit;
00021     }
00022 
00028     public static function dump($data)
00029     {
00030         echo "<pre class=\"vardump\">";
00031         var_dump($data);
00032         echo "</pre>";
00033     }
00034 
00048     public static function setUrlParam($url, $name, $value)
00049     {
00050         //does it already have any stuff?
00051         if ($pos = strpos($url, '?'))
00052         {
00053             $vars = substr($url, $pos+1);
00054             $url = substr($url, 0, $pos);
00055         }
00056 
00057         //get our params...
00058         $getAr = array();
00059         $paramAr = explode('&', $vars);
00060         foreach ($paramAr AS $param)
00061         {
00062             if ($param)
00063             {
00064                 $vars = explode('=', $param);
00065                 $getAr[$vars[0]] = $vars[1];
00066             }
00067         }
00068 
00069         //set our new one...
00070         $getAr[$name] = urlencode($value);
00071         
00072         //now format that url....
00073         $count = 0;
00074         $url .= "?";
00075         foreach ($getAr AS $key => $val)
00076         {
00077             if ($count)
00078                 $url .= '&';
00079                 
00080             $url .= "$key=$val";
00081         
00082             $count++;
00083         }
00084 
00085         return $url;
00086     }
00087 
00093     public static function isValidEmail($email)
00094     {
00095         $regexp = "/^[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}$/i";
00096         return preg_match($regexp, $email);
00097     }
00098 
00099 
00114     public static function ieDrawClear()
00115     {
00116         echo "\t<div id=\"clear\"><!-- if you use IE, shame on you! --></div>\n";
00117     }
00118 
00127     public static function spawnHttpAsync($page)
00128     {
00129         $cbSock = fsockopen(Config::get('site_hostname'), 80, $errno, $errstr, 5);
00130         if ($cbSock)
00131         {
00132             $cmd  = "GET {$page} HTTP/1.0\r\n";
00133             $cmd .= "Host: " . Config::get('site_hostname') . "\r\n\r\n";
00134             fwrite($cbSock, $cmd);
00135         }
00136     }
00137 
00147     public static function getUrlContents($link)
00148     {
00149         $ch = curl_init();
00150         $timeout = 5; 
00151         curl_setopt ($ch, CURLOPT_URL, $link);
00152         curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
00153         curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
00154         $file_contents = curl_exec($ch);
00155         curl_close($ch);
00156 
00157         return $file_contents;
00158     }
00159 
00173     public static function getBlurb($text, $length = 255, &$truncated = null)
00174     {
00175         //clean it up.
00176         $text = strip_tags(trim($text));
00177 
00178         //if its shorter or exactly ourlength... use it.
00179         if (strlen($text) <= $length)
00180         {
00181             $blurb = $text;
00182             $truncated = false;
00183         }
00184         //nope, we might truncate
00185         else
00186         {
00187             //search for first space.
00188             $pos = strpos($text, " ", $length);
00189             
00190             //if it didnt get found... it is the thing.
00191             if ($pos === false)
00192             {
00193                 $blurb = $text;
00194                 $truncated = false;
00195             }
00196             else
00197             {
00198                 $blurb = substr($text, 0, $pos);
00199                 $truncated = true;
00200             }
00201         }
00202 
00203         //send it back formatted.
00204         return Linkify::bbcode($blurb);
00205     }
00206 
00213     public static function getNumberSuffix($n)
00214     {
00215         $l = substr($n, -1);
00216         $m = substr($n, -2);
00217         
00218         if ($m == '11')
00219             return $n . "th";
00220         if ($m == '12')
00221             return $n . "th";
00222         if ($m == '13')
00223             return $n . "th";
00224         if ($l == 1)
00225             return $n . "st";
00226         if ($l == 2)
00227             return $n . "nd";
00228         if ($l == 3)
00229             return $n . "rd";
00230         else
00231             return $n . "th";
00232     }
00233 
00234     public static function formatPhoneNumber($number)
00235     {
00236         //remove non number characters
00237         $number = preg_replace("/[^0-9]/", "", $number);
00238 
00239         if (preg_match("/([0-9]{3})([0-9]{3})([0-9]{4})/", $number, $matches))
00240             return "$matches[1].$matches[2].$matches[3]";
00241         
00242         return $number;
00243     }
00244 
00245     public static function shellExec($call)
00246     {
00247         return system($call);
00248     }
00249 
00253     public static function noCache()
00254     {
00255         // Date in the past
00256         header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
00257         
00258         // Always modified
00259         header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
00260         
00261         // HTTP/1.1
00262         header("Cache-Control: no-store, no-cache, must-revalidate");
00263         header("Cache-Control: pre-check=0, post-check=0", false);
00264         
00265         // HTTP/1.0
00266         header("Pragma: no-cache");
00267     }
00268 
00283     public static function pluralize($str, $amount = null, $num = false)
00284     {
00285         if ($num)
00286         {
00287             if ($amount)
00288             {
00289                 if ($num === -1)
00290                 {
00291                     $whole = (int)$amount;
00292                     $prefix = $whole ? $whole . " " : "";
00293 
00294                     $frac = $amount - $whole;
00295                     if ($frac)
00296                     {
00297                         if ($frac == 0.25)
00298                             $prefix .= "&frac14; ";
00299                         else if ($frac == 0.50)
00300                             $prefix .= "&frac12; ";
00301                         else if ($frac == 0.75)
00302                             $prefix .= "&frac34; ";
00303                         else
00304                             $prefix = "$amount ";
00305                     }
00306                 }
00307                 else
00308                     $prefix = "$amount ";
00309             }
00310             else
00311                 $prefix = "0 ";
00312         }
00313         else
00314             $prefix = "";
00315 
00316         if ($amount !== null && abs($amount) == 1)
00317             return $prefix . $str;
00318 
00319         // Simple pluralization rules.  Add more rules or exceptions as needed.
00320 
00321         if (substr($str, -1) == "s")
00322             return $prefix . $str . "es";
00323         if (substr($str, -1) == "y" && $str != 'day')
00324             return $prefix . substr($str, 0, -1) . "ies";
00325         if (substr($str, -1) == "f")
00326             return $prefix . substr($str, 0, -1) . "ves";
00327 
00328         return $prefix . $str . "s";
00329     }
00330 }
00331 ?>

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