00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00020 final class TextUtils extends StaticFactory
00021 {
00022 public static function friendlyFileSize(
00023 $size, $precision = 2,
00024 $units = array(null, 'k' , 'M', 'G', 'T', 'P'),
00025 $spacePunctuation = false
00026 )
00027 {
00028 if ($size > 0) {
00029 $index = min((int) log($size, 1024), count($units) - 1);
00030
00031 return
00032 round($size / pow(1024, $index), $precision)
00033 .($spacePunctuation ? ' ' : null)
00034 .$units[$index];
00035 }
00036
00037 return 0;
00038 }
00039
00040 public static function getRootFromUrl($url)
00041 {
00042 if (
00043 strpos($url, '
00044 && (strpos($url, '
00045 )
00046 $offset = strpos($url, '
00047 else
00048 $offset = 0;
00049
00050 return substr($url, 0, strpos($url, '/', $offset) + 1);
00051 }
00052
00053 public static function getPathFromUrl($url)
00054 {
00055 $parsed = parse_url($url);
00056
00057 if ($parsed === false or !isset($parsed['path']))
00058 return '/';
00059 else
00060 return $parsed['path'];
00061 }
00062
00063 public static function urlSafeBase64Encode($string)
00064 {
00065 return
00066 str_replace(
00067 array('+', '/' , '='),
00068 array('-', '_', ''),
00069 base64_encode($string)
00070 );
00071 }
00072
00073 public static function urlSafeBase64Decode($string)
00074 {
00075 $data = str_replace(
00076 array('-', '_'),
00077 array('+', '/'),
00078 $string
00079 );
00080
00081 $mod4 = strlen($data) % 4;
00082
00083 if ($mod4) {
00084 $data .= substr('====', $mod4);
00085 }
00086
00087 return base64_decode($data);
00088 }
00089
00090 public static function upFirst($string)
00091 {
00092 $firstOne = mb_strtoupper(mb_substr($string, 0, 1));
00093
00094 return $firstOne.mb_substr($string, 1);
00095 }
00096
00097 public static function downFirst($string)
00098 {
00099 $firstOne = mb_strtolower(mb_substr($string, 0, 1));
00100
00101 return $firstOne.mb_substr($string, 1);
00102 }
00103
00104 public static function cutOnSpace($string, $length, $append = null)
00105 {
00106 $stringLength = mb_strlen($string);
00107
00108 if ($stringLength < $length)
00109 return $string;
00110 else {
00111 if (!$pos = mb_strpos($string, ' ', $length))
00112 $pos = $stringLength;
00113
00114 return mb_substr($string, 0, $pos).$append;
00115 }
00116 }
00117
00121 public static function normalizeUri($uri)
00122 {
00123 return GenericUri::create()->
00124 parse($uri, true)->
00125 normalize()->
00126 toString();
00127 }
00128
00129 public static function hex2Binary($hex)
00130 {
00131 $length = strlen($hex);
00132
00133 Assert::isTrue( $length % 2 == 0);
00134
00135 $out = null;
00136 for ($i = 0; $i < $length; $i += 2) {
00137 $out .= pack('C', hexdec(substr($hex, $i, 2)));
00138 }
00139
00140 return $out;
00141 }
00142
00151 public static function safeAmp($text)
00152 {
00153 $result = preg_replace(
00154 '/&(?!(#(([0-9]+)|(x[0-9A-F]+))'
00155 .'|([a-z][a-z0-9]*));)/i',
00156 '&',
00157 $text
00158 );
00159
00160 return $result;
00161 }
00162
00163 public static function friendlyNumber($integer, $delimiter = ' ')
00164 {
00165 Assert::isInteger($integer);
00166
00167 if ($integer > 9999) {
00168 $orders = array();
00169
00170 while ($integer > 0) {
00171 $order = $integer % 1000;
00172 $integer = (int) ($integer / 1000);
00173
00174 if ($integer > 0)
00175 $orders[] = sprintf('%03d', $order);
00176 else
00177 $orders[] = (string) $order;
00178 }
00179
00180 $result = implode($delimiter, array_reverse($orders));
00181
00182 } else
00183 $result = (string) $integer;
00184
00185 return $result;
00186 }
00187 }
00188 ?>