TextUtils.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2006-2009 by Anton E. Lebedevich                        *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  *    UrlSaveBase64* functions borrowed from comments on                   *
00011  *    http://www.php.net/manual/en/function.base64-encode.php              *
00012  *    by massimo dot scamarcia at gmail dot com                            *
00013  *                                                                         *
00014  ***************************************************************************/
00015 
00019     final class TextUtils extends StaticFactory
00020     {
00021         public static function friendlyFileSize(
00022             $size, $precision = 2,
00023             $units = array(null, 'k' , 'M', 'G', 'T', 'P'),
00024             $spacePunctuation = false
00025         )
00026         {
00027             if ($size > 0) {
00028                 $index = min((int) log($size, 1024), count($units) - 1);
00029                 
00030                 return
00031                     round($size / pow(1024, $index), $precision)
00032                     .($spacePunctuation ? ' ' : null)
00033                     .$units[$index];
00034             }
00035             
00036             return 0;
00037         }
00038         
00039         public static function getRootFromUrl($url)
00040         {
00041             if (
00042                 strpos($url, '//') !== false
00043                 && (strpos($url, '//') + 2) < strlen($url)
00044             )
00045                 $offset = strpos($url, '//') + 2;
00046             else
00047                 $offset = 0;
00048             
00049             return substr($url, 0, strpos($url, '/', $offset) + 1);
00050         }
00051         
00052         public static function getPathFromUrl($url)
00053         {
00054             $parsed = parse_url($url);
00055             
00056             if ($parsed === false or !isset($parsed['path']))
00057                 return '/';
00058             else
00059                 return $parsed['path'];
00060         }
00061         
00062         public static function urlSafeBase64Encode($string)
00063         {
00064             return
00065                 str_replace(
00066                     array('+', '/' , '='),
00067                     array('-', '_', ''),
00068                     base64_encode($string)
00069                 );
00070         }
00071         
00072         public static function urlSafeBase64Decode($string)
00073         {
00074             $data = str_replace(
00075                 array('-', '_'),
00076                 array('+', '/'),
00077                 $string
00078             );
00079             
00080             $mod4 = strlen($data) % 4;
00081             
00082             if ($mod4) {
00083                 $data .= substr('====', $mod4);
00084             }
00085             
00086             return base64_decode($data);
00087         }
00088         
00089         public static function upFirst($string)
00090         {
00091             $firstOne = mb_strtoupper(mb_substr($string, 0, 1));
00092             
00093             return $firstOne.mb_substr($string, 1);
00094         }
00095         
00096         public static function downFirst($string)
00097         {
00098             $firstOne = mb_strtolower(mb_substr($string, 0, 1));
00099             
00100             return $firstOne.mb_substr($string, 1);
00101         }
00102         
00103         public static function cutOnSpace($string, $length, $append = null)
00104         {
00105             $stringLength = mb_strlen($string);
00106             
00107             if ($stringLength < $length)
00108                 return $string;
00109             else {
00110                 if (!$pos = mb_strpos($string, ' ', $length))
00111                     $pos = $stringLength;
00112                 
00113                 return mb_substr($string, 0, $pos).$append;
00114             }
00115         }
00116         
00120         public static function normalizeUri($uri)
00121         {
00122             return GenericUri::create()->
00123                 parse($uri, true)->
00124                 normalize()->
00125                 toString();
00126         }
00127         
00128         public static function hex2Binary($hex)
00129         {
00130             $length = strlen($hex);
00131             
00132             Assert::isEqual($length % 2, 0);
00133             
00134             $out = null;
00135             for ($i = 0; $i < $length; $i += 2) {
00136                 $out .= pack('C', hexdec(substr($hex, $i, 2)));
00137             }
00138             
00139             return $out;
00140         }
00141         
00150         public static function safeAmp($text)
00151         {
00152             $result = preg_replace(
00153                 '/&(?!(#(([0-9]+)|(x[0-9A-F]+))'
00154                 .'|([a-z][a-z0-9]*));)/i',
00155                 '&amp;',
00156                 $text
00157             );
00158             
00159             return $result;
00160         }
00161         
00162         public static function friendlyNumber($number, $delimiter = ' ')
00163         {
00164             $localeInfo = localeconv();
00165             
00166             $decimalPoint = $localeInfo['decimal_point'];
00167             
00168             $number = (string) $number;
00169             
00170             $parts = explode($decimalPoint, $number);
00171             
00172             $integer = abs(array_shift($parts));
00173             
00174             $minus = $number < 0 ? '-' : '';
00175             
00176             $floatDiff = array_shift($parts);
00177             
00178             if ($integer > 9999) {
00179                 $orders = array();
00180                 
00181                 while ($integer > 0) {
00182                     $order = $integer % 1000;
00183                     $integer = (int) ($integer / 1000);
00184                     
00185                     if ($integer > 0)
00186                         $orders[] = sprintf('%03d', $order);
00187                     else
00188                         $orders[] = (string) $order;
00189                 }
00190                 
00191                 $result = implode($delimiter, array_reverse($orders));
00192                 
00193             } else
00194                 $result = (string) $integer;
00195             
00196             if ($floatDiff)
00197                 $result = $result.$decimalPoint.$floatDiff;
00198             
00199             return $minus.$result;
00200         }
00201     }
00202 ?>