3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php class __Utils { public static function setHeader(string $header, $variable, $replace = false, $httpresponse = null) { if($httpresponse !== null) { header("$header: $variable", $replace, $httpresponse); } else { header("$header: $variable", $replace); } } public static function setHeaders(array $headers) { foreach($headers as $header => $variables){ if(is_array($variables)) { if(isset($variables[1]) && self:isBoolean($variables[1])) $replace = $variables[1]; else $replace = false; if(isset($variables[2]) && self::isInteger($variables[2])) { self::setHeader($header, $variables[0], $replace, $variables[2]); } else { self::setHeader($header, $variables[0], $replace); } } else { self:setHeader($header, $variables); } } } public static function spaces($count, $htmlentities = true) { $spaces = str_repeat('&nbsp;', (int) $count); return ($htmlentities) ? $spaces : str_replace('&nbsp;', '', $spaces); } public static function tabs($count, $tabsize = 6) { return self::spaces(((int) $tabsize * (int) $count)); } public static function lineBreaks($count, $heightpx = 16) { return str_repeat('<br style="height: ' . $heightpx . 'px;" />', (int) $count); } public static function isArray($variable) { return (is_array($variable)); } public static function isBoolean($variable) { return (is_bool($variable)); } public static function isString($variable) { return (is_string($variable)); } public static function isNull($variable) { return (is_null($variable)); } public static function isCallable($variable) { return (is_callable($variable)); } public static function isObject($variable) { return (is_object($variable)); } public static function isDouble($variable) { return (is_double($variable)); } public static function isFloat($variable) { return (is_float($variable)); } public static function isInteger($variable) { return (is_int($variable)); } public static function isLong($variable) { return (is_long($variable)); } public static function isResource($variable) { return (is_resource($variable)); } public static function getVarType($variable) { $typecases = array( "array" => self::isArray($variable), "boolean" => self::isBoolean($variable), "string" => self::isString($variable), "null" => self::isNull($variable), "callable" => self::isCallable($variable), "object" => self::isObject($variable), "double" => self::isDouble($variable), "integer" => self::isInteger($variable), "float" => self::isFloat($variable), "long" => self::isLong($variable), "resource" => self::isResource($variable) ); foreach($typecases as $type => $bool){ if($bool) return $type; } } public static function nocacheHaders() { if (!headers_sent()) { self::setHeaders(array( "Expires" => "Wed, 11 Jan 1984 05:00:00 GMT", "Last-Modified" => gmdate( 'D, d M Y H:i:s' ) . " GMT", "Cache-Control" => "no-cache, must-revalidate, max-age=0", "Pragma" => "no-cache", )); } } public static function stripSpace(string $string) { return str_replace(' ', '', preg_replace('/\s+/', '', $string)); } public static function sanitize(string $string) { $sanitized = $string; $sanitized = strip_tags($sanitized); $sanitized = htmlspecialchars($sanitized, ENT_QUOTES, 'utf-8'); $sanitized = htmlentities($sanitized, ENT_HTML5, 'utf-8'); return $sanitized; } public static function dumpVar($variable, $doreturn = false, $dopre = true, $styles = true, $tabamt = 1) { $return = ''; $wrapper = ($dopre === null) ? 'none' : ((self::isBoolean($dopre)) ? (($dopre) ? 'pre' : 'div' ) : $dopre ); $class = ($styles) ? 'styled' : 'nostyles'; $settings = array( 'wrappers' => array( 'none' => '%s', 'pre' => '<pre class="vardump ' . $class . '">%s</pre>', 'div' => '<div class="vardump ' . $class . '">%s</div>' ), 'sprintf' => array( 'vartype' => '<span class="vartype">%s</span>', 'specialvartype' => '<span class="specialvartype">%s</span>', 'parentheses' => '<span class="parentheses">(</span><span class="parcont">%s</span><span class="parentheses">)</span>', 'string' => '<span class="misc">"%s"</span>', 'raw' => '<span class="misc">%s</span>', 'arrkey' => '<span class="misc">' . self::tabs($tabamt) . '[%s]</span>', 'arrsep' => '<span class="arrsep">=></span>' ), ); switch(self::getVarType($variable)) { case "boolean": $return .= sprintf($settings['sprintf']['vartype'], 'bool') . sprintf($settings['sprintf']['parentheses'], (string) $variable ? 'true' : 'false'); break; case "integer": $return .= sprintf($settings['sprintf']['vartype'], 'int') . sprintf($settings['sprintf']['parentheses'], (string) $variable); break; case "double": $return .= sprintf($settings['sprintf']['vartype'], 'double') . sprintf($settings['sprintf']['parentheses'], (string) $variable); break; case "float": $return .= sprintf($settings['sprintf']['vartype'], 'float') . sprintf($settings['sprintf']['parentheses'], (string) $variable); break; case "string": $return .= sprintf($settings['sprintf']['vartype'], 'string') . sprintf($settings['sprintf']['parentheses'], (string) strlen($variable)) . " " . sprintf($settings['sprintf']['string'], $variable); break; case "null": $return .= sprintf($settings['sprintf']['specialvartype'], 'NULL'); break; case "array": $brackets = array('{'); foreach($variable as $key => $value) { $brackets[] = sprintf($settings['sprintf']['arrkey'], $key) . " " . $settings['sprintf']['arrsep'] . " " . self::dumpVar($value, true, null, $styles, $tabamt + 1) . ','; } $brackets[] = ($tabamt < 2) ? '}' : self::tabs(($tabamt - 1)) . '}'; $brackets = ($dopre) ? implode("\n", $brackets) : implode('<br />', $brackets); $return .= sprintf($settings['sprintf']['vartype'], 'array') . sprintf($settings['sprintf']['parentheses'], (string) count($variable)) . " " . $brackets; break; case "object": $reflclass = new ReflectionClass($variable); $constants = $reflclass->getConstants(); $variables = $reflclass->getProperties(); $methods = $reflclass->getMethods(); $brackets = array('{'); foreach($constants as $name => $value) { $brackets[] = self::tabs($tabamt) . sprintf($settings['sprintf']['raw'], 'CONST ' . $name) . ' ' . self::dumpVar($value, true, null, $styles); } foreach($variables as $var) { } $return .= sprintf($settings['sprintf']['vartype'], 'object') . sprintf($settings['sprintf']['parentheses'], get_class($variable)); break; default: $return .= sprintf($settings['sprintf']['specialvartype'], 'UNKNOWN TYPE') . ' ' . sprintf($settings['sprintf']['raw'], var_export($variable, true)); break; } if($dopre) $return = str_ireplace('&nbsp;', ' ', $return); $returnwrapper = (array_key_exists($wrapper, $settings['wrappers'])) ? $settings['wrappers'][$wrapper] : $settings['wrappers']['none']; $return = sprintf($returnwrapper, $return); if($doreturn) return $return; echo $return; } public static function dumpVars($variables, $doreturn = false, $dopre = true, $styles = true, $tabamt = 1) { $return = array(); foreach($variables as $var) { $return[] = self::dumpVar($var, true, $dopre, $styles, $tabamt); } $return = implode(self::lineBreaks(1), $return); if($doreturn) return $return; echo $return; } public static function outputData(string $filename, string $content = null, $mimetype = "auto") { $mimetype = (is_file($filename)) ? finfo_file(finfo_open(FILEINFO_MIME, pathinfo($content, PATHINFO_DIRNAME), $content)) : ($mimetype == "auto") ? "application/force-download" : $mimetype; if(!headers_sent()) { $output = is_null($content) ? file_get_contents($file) : $content; self:setHeaders(array( "Pragma" => "public", "Expires" => "0", "Cache-Control" => "must-revalidate, post-check=0, pre-check=0", "Cache-Control" => array("private", FALSE), "Content-Disposition" => "attatchment; filename=" . basename(str_replace('"', '', $filename)) . '";', "Content-Type" => $mimetype, "Content-Transer-Encoding" => "binary", "Content-Length" => strlen($output), )); ob_clean(); flush(); echo $output; exit(); } } public static function forceDownload($file, $content = null) { self::outputData($file, $content, "application/force-download"); } public static function uniqueId($prefix = null, $format = '{35}') // Ex: {8}-{4}-{4}-{4}-{12} will return something like XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX { $uniqueid = $prefix; $alpha = range('a', 'z', 1); $alphacase = range('A', 'Z', 1); $num = range('1', '999', 1); $rand1 = array_rand($alpha, 5); $rand2 = array_rand($alphacase, 5); $rand3 = array_rand($num, 5); $rand = array_merge($rand1, $rand2, $rand3); $datetime = new DateTime('now', new DateTimeZone('America/New_York')); $timestamp = $datetime->format('c'); $uniqueid .= implode('', $rand) . "_" . $timestamp . "_" . uniqid(); $origuniqueid = $uniqueid; foreach(hash_algos() as $algo) { $uniqueid = hash($algo, $uniqueid . "_" . $timestamp . "_" . uniqid()); } $uniqueid = md5($uniqueid); $chararrays = array(); $finaluniqueid = ''; $amount = 10; for($time = $amount; $time > 0; $time--) { $chararrays[$time] = str_split($uniqueid); shuffle($chararrays[$time]); $finaluniqueid .= implode('', array_slice($chararrays[$time], rand(0, (count($chararrays[$time]) - 1)), rand(0, (count($chararrays[$time]) - 1)))); } $finaluniqueid = str_shuffle($finaluniqueid) . str_shuffle($finaluniqueid) . str_shuffle($finaluniqueid) . str_shuffle($finaluniqueid) . str_shuffle($finaluniqueid); $formateduniqueid = $format; $substart = 0; preg_match_all('([0-9]+)', $format, $ints); foreach($ints[0] as $int) { if($newi > strlen($formateduniqueid) ) $newi = strlen($formateduniqueid); $substr = substr($finaluniqueid, $substart, ((int) $int)); $formateduniqueid = preg_replace('/\{' . $int . '\}/', $substr, $formateduniqueid, 1); $substart = $substart + ((int) $int); } return $formateduniqueid; } public static function getRelativePath($from, $to) { $from = is_dir($from) ? rtrim($from, '\/') . '/' : $from; $to = is_dir($to) ? rtrim($to, '\/') . '/' : $to; $from = str_replace('\\', '/', $from); $to = str_replace('\\', '/', $to); $from = explode('/', $from); $to = explode('/', $to); $relPath = $to; foreach($from as $depth => $dir) { if($dir === $to[$depth]) { array_shift($relPath); } else { $remaining = count($from) - $depth; if($remaining > 1) { $padLength = (count($relPath) + $remaining - 1) * -1; $relPath = array_pad($relPath, $padLength, '..'); break; } else { $relPath[0] = './' . $relPath[0]; } } } return implode('/', $relPath); } public static function arrayList(array $array, $withkeys = false, $keysep = ': ') { if($withkeys) { foreach($array as $k => $v) { $array[$k] = $k . $keysep . $v; } } switch(count($array)) { case 1: return implode('', $array); break; case 2: return implode(' and ', $array); break; case 3: $arrv = array_values($array); $array[array_search($arrv[(count($arrv) - 1)], $array)] = 'and ' . $array[array_search($arrv[(count($arrv) - 1)], $array)]; $output = implode(', ', $array); if(strtoupper(Locale::getRegion(Locale::getDefault())) === 'GB') $output = str_replace(', and', ' and ', $output); return $output; } } public static function pluckArrays($key, $value, $arrays) { $return = array(); foreach($arrays as $array) { if($array[$key] === $value) { $return[] = $array; } } return $return; } public static function addQuotes($string, $quote = '"') { return $quote . $string . $quote; } }
based on H1aPZ
Output for 5.1.0 - 5.4.32
Parse error: syntax error, unexpected ':' in /in/irfii on line 18
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_ARRAY, expecting '&' or T_VARIABLE in /in/irfii on line 14
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/irfii on line 5
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/irfii on line 5
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/irfii on line 5
Process exited with code 255.