Skip to content

Instantly share code, notes, and snippets.

@krizpoon
Last active August 29, 2015 14:07
Show Gist options
  • Save krizpoon/059d40ad17b0c294d933 to your computer and use it in GitHub Desktop.
Save krizpoon/059d40ad17b0c294d933 to your computer and use it in GitHub Desktop.
PHP snippets
if (!function_exists('boolval'))
{
function boolval($b)
{
if (is_bool($b)) return $b;
if ($b == null) return false;
if (is_string($b))
{
if (!$b) return false;
$ch = strtolower($b[0]);
return ($ch == 't') || ($ch == 'y') || intval($ch) > 0;
}
else if (is_numeric($b))
{
return $b != 0;
}
return true;
}
}
header('Content-Type: application/json');
ini_set('display_errors',1);
error_reporting(E_ALL);
function getBaseUrl()
{
$port = $_SERVER['SERVER_PORT'];
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $port == 443) ? "https://" : "http://";
$domain = $_SERVER['HTTP_HOST'];
$path = dirname($_SERVER["REQUEST_URI"]);
return $protocol.$domain.($port == 80 || $port == 443? '': ':'.$port).$path;
}
// Get content of associative array safely, with optional default value
function val($array, $key, $default = null)
{
if (!$array) return $default;
if (!isset($array[$key])) return $default;
return $array[$key];
}
function die404($msg = null)
{
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found", true, 404);
header("Status: 404 Not Found");
$_SERVER['REDIRECT_STATUS'] = 404;
echo $msg? $msg: 'Your requested resource is not found';
die();
}
function die500($msg = null)
{
header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
echo $msg? $msg: 'An internal server error has occurred.';
die();
}
function TSUtf8CharFromNumber($num)
{
if($num<=0x7F) return chr($num);
if($num<=0x7FF) return chr(($num>>6)+192).chr(($num&63)+128);
if($num<=0xFFFF) return chr(($num>>12)+224).chr((($num>>6)&63)+128).chr(($num&63)+128);
if($num<=0x1FFFFF) return chr(($num>>18)+240).chr((($num>>12)&63)+128).chr((($num>>6)&63)+128).chr(($num&63)+128);
return '';
}
function TSUnescapeUtf8Char($text)
{
return preg_replace_callback('/\\\\u([0-9a-f]{4})/i', create_function('$matches', 'return TSUtf8CharFromNumber(hexdec($matches[1]));'), $text);
}
function normalizeSpaces($str)
{
return preg_replace('!\s+!', ' ', trim($str));
}
function hasPrefix($haystack, $needle)
{
return $needle === '' || strpos($haystack, $needle) === 0;
}
function hasSuffix($haystack, $needle)
{
return $needle === '' || substr($haystack, -strlen($needle)) === $needle;
}
/**
* Generate VALID RFC 4211 COMPLIANT Universally Unique IDentifiers (UUID) version 4. Version 4 UUIDs are pseudo-random.
*/
function uuidv4()
{
return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
// 32 bits for "time_low"
mt_rand(0, 0xffff), mt_rand(0, 0xffff),
// 16 bits for "time_mid"
mt_rand(0, 0xffff),
// 16 bits for "time_hi_and_version",
// four most significant bits holds version number 4
mt_rand(0, 0x0fff) | 0x4000,
// 16 bits, 8 bits for "clk_seq_hi_res",
// 8 bits for "clk_seq_low",
// two most significant bits holds zero and one for variant DCE1.1
mt_rand(0, 0x3fff) | 0x8000,
// 48 bits for "node"
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment