30 const VSTATIC_CONST = 0x01;
33 const VSTATIC_VAR = 0x02;
62 $bt = debug_backtrace();
63 $caller = isset($bt[2][
'object']) ? $bt[2][
'object'] : null;
84 return (
bool) preg_match(
86 [\x09\x0A\x0D\x20-\x7E] # ASCII 87 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte 88 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs 89 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte 90 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates 91 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 92 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 93 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 124 public static function toUTF8($text, $from =
'iso-8859-1')
126 $alreadyEncoded = self::isUTF8($text);
129 if ($from ==
'__double') {
130 if ($alreadyEncoded) {
136 if (function_exists(
'utf8_decode')) {
137 $res = utf8_decode($text);
138 } elseif (function_exists(
'iconv')) {
139 $res = iconv(
'utf-8',
'iso-8859-1//TRANSLIT', $text);
140 } elseif (function_exists(
'recode')) {
141 $res = recode(
'utf-8..iso-8859-1', $text);
142 } elseif (function_exists(
'mb_convert_encoding')) {
143 $res = mb_convert_encoding($text,
'iso-8859-1',
'utf-8');
144 } elseif (function_exists(
'html_entity_decode')) {
145 $res = html_entity_decode(
146 htmlentities($text, ENT_QUOTES,
'utf-8'),
153 return (self::isUTF8($res) ? $res : $text);
158 $from =
'iso-8859-1';
161 if ($alreadyEncoded) {
165 if (!strcasecmp($from,
'iso-8859-1') &&
166 function_exists(
'utf8_encode')) {
167 return utf8_encode($text);
170 if (function_exists(
'iconv')) {
171 return iconv($from,
'UTF-8//TRANSLIT', $text);
174 if (function_exists(
'recode')) {
175 return recode($from.
'..utf-8', $text);
178 if (function_exists(
'mb_convert_encoding')) {
179 return mb_convert_encoding($text,
'UTF-8', $from);
182 if (function_exists(
'html_entity_decode')) {
183 return html_entity_decode(
184 htmlentities($text, ENT_QUOTES, $from),
190 throw new \Erebot\NotImplementedException(
'No way to convert to UTF-8');
221 $source = self::VSTATIC_CONST
223 if (is_object($class)) {
224 $class = get_class($class);
226 $refl = new \ReflectionClass($class);
228 if (($source & self::VSTATIC_CONST) == self::VSTATIC_CONST) {
230 return $refl->getConstant($name);
231 }
catch (ReflectionException $e) {
235 if (($source & self::VSTATIC_VAR) == self::VSTATIC_VAR) {
237 $reflProp = $refl->getProperty($name);
238 return $reflProp->getValue();
239 }
catch (ReflectionException $e) {
243 throw new \Erebot\NotFoundException(
'No such thing');
259 if (is_string($item)) {
262 if (is_object($item) && method_exists($item,
'__toString')) {
280 public static function humanSize($size, $max = null, $system =
'si', $retstring =
'%01.2f %s')
284 $systems[
'si'][
'suffix'] = array(
'B',
'KB',
'MB',
'GB',
'TB',
'PB');
285 $systems[
'si'][
'size'] = 1000;
286 $systems[
'bi'][
'suffix'] = array(
'B',
'KiB',
'MiB',
'GiB',
'TiB',
'PiB');
287 $systems[
'bi'][
'size'] = 1024;
288 $sys = isset($systems[$system]) ? $systems[$system] : $systems[
'si'];
291 $depth = count($sys[
'suffix']) - 1;
292 if ($max &&
false !== $d = array_search($max, $sys[
'suffix'])) {
298 while ($size >= $sys[
'size'] && $i < $depth) {
299 $size /= $sys[
'size'];
303 return sprintf($retstring, $size, $sys[
'suffix'][$i]);
321 $size = (float) str_replace(
",",
".", $humanSize);
322 $suffix = (string) substr(
324 strspn($humanSize,
"1234567890.,+-")
326 $suffix = trim($suffix);
327 $exponents = array_flip(array(
'',
'K',
'M',
'G',
'T',
'P'));
330 switch (strlen($suffix)) {
340 if ($suffix[1] !=
'i') {
343 $suffix = $suffix[0].$suffix[1];
348 if (!isset($exponents[$suffix[0]])) {
351 $exp = $exponents[$suffix[0]];
353 return (
int) ($size * pow($base, $exp));
Utility methods for Erebot.
static getVStatic($class, $name, $source=self::VSTATIC_CONST)
static stringifiable($item)
static parseHumanSize($humanSize)
static humanSize($size, $max=null, $system= 'si', $retstring= '%01.2f%s')
static toUTF8($text, $from= 'iso-8859-1')