Skip to content

Instantly share code, notes, and snippets.

@sameei
Last active August 29, 2015 14:01
Show Gist options
  • Save sameei/f18dffb7238c0568066a to your computer and use it in GitHub Desktop.
Save sameei/f18dffb7238c0568066a to your computer and use it in GitHub Desktop.
helpers in php-lang
<?php # in the name of ALLAH
/*
* Ord and Chr function for convert num to uni-cahr and ...
* reference: http://stackoverflow.com/questions/9361303/can-i-get-the-unicode-value-of-a-character-or-vise-versa-with-php
*/
function _uniord($c) {
if (ord($c{0}) >=0 && ord($c{0}) <= 127)
return ord($c{0});
if (ord($c{0}) >= 192 && ord($c{0}) <= 223)
return (ord($c{0})-192)*64 + (ord($c{1})-128);
if (ord($c{0}) >= 224 && ord($c{0}) <= 239)
return (ord($c{0})-224)*4096 + (ord($c{1})-128)*64 + (ord($c{2})-128);
if (ord($c{0}) >= 240 && ord($c{0}) <= 247)
return (ord($c{0})-240)*262144 + (ord($c{1})-128)*4096 + (ord($c{2})-128)*64 + (ord($c{3})-128);
if (ord($c{0}) >= 248 && ord($c{0}) <= 251)
return (ord($c{0})-248)*16777216 + (ord($c{1})-128)*262144 + (ord($c{2})-128)*4096 + (ord($c{3})-128)*64 + (ord($c{4})-128);
if (ord($c{0}) >= 252 && ord($c{0}) <= 253)
return (ord($c{0})-252)*1073741824 + (ord($c{1})-128)*16777216 + (ord($c{2})-128)*262144 + (ord($c{3})-128)*4096 + (ord($c{4})-128)*64 + (ord($c{5})-128);
if (ord($c{0}) >= 254 && ord($c{0}) <= 255) // error
return FALSE;
return 0;
} // function _uniord()
function _unichr($o) {
if (function_exists('mb_convert_encoding')) {
return mb_convert_encoding('&#'.intval($o).';', 'UTF-8', 'HTML-ENTITIES');
} else {
return chr(intval($o));
}
} // function _unichr()
/*
* Deteccting file size overflow in php-post
* references:
* http://stackoverflow.com/questions/22877651/how-to-detect-phps-file-upload-error-when-files-is-empty
* http://stackoverflow.com/questions/11738949/php-warning-post-content-length-of-n-bytes-exceeds-the-limit-of-3145728-bytes-i/11745361#11745361
* http://andrewcurioso.com/2010/06/detecting-file-size-overflow-in-php/
*/
function _isPostOverflow() {
$postMaxSize = ini_get('post_max_size');
$sizeUnit = substr($postMaxSize, -1);
switch( $sizeUnit ) {
// in this way if $sizeUnit == G, all of them(G,M,K) will be applied.
// and G,M,K in postMaxSize string don't applied in statements!
case 'G': $postMaxSize = $postMaxSize * 1024;
case 'M': $postMaxSize = $postMaxSize * 1024;
case 'K': $postMaxSize = $postMaxSize * 1024;
}
if( $_SERVER['REQUEST_METHOD'] == 'POST'
&& empty( $_POST )
&& empty( $_FILES )
&& $_SERVER['CONTENT_LENGTH'] > $postMaxSize ) return true;
return false;
}
/**
* @return void
* dump(...)
*/
function dump()
{
echo "<pre>";
call_user_func_array("var_dump", func_get_args());
echo "</pre>";
}
/**
* @return void
* @param Exception $e
*/
function excdump(Exception $e)
{
$type = get_class($e);
echo "Exception({$type}): {$e->getMessage()}";
echo "<br/>\nIn({$e->getFile()} # {$e->getLine()})";
dump($e->getTraceAsString());
}
function echoArray($array)
{
echo "<pre>";
print_r($array);
echo "</pre>";
}
function clsdump($obj)
{
$type = get_class($obj);
echo "instance of {$type}: </br>\n <pre>";
print_r(get_class_methods($obj));
echo "</pre>";
}
function useOf($link)
{
list($module, $controller, $action)
= array_map(function($str){
return str_replace( "-", "", ucwords( $str ) );
}, explode("/", $link)
);
$action = lcfirst($action);
return "{$module}\\Controllers\\{$controller}::{$action}";
}
function pathOf($file)
{
return realpath( dirname( $file ) );
}
function exho($val, $label=null)
{
if( !is_null($label) ) echo "<span style='color:red;'>{$label}:</span></br>";
echo "<pre>";
if( is_array($val) ) print_r ($val);
elseif( is_resource($val) ) echo "Resource: ", $val;
elseif( is_object($val) )
{
clsdump($val);
if( method_exists($val, 'toArray') ) print_r($val->toArray());
}
else var_dump( $val ); // null, bool, string, number
echo "</pre>";
}
function memOf($val)
{
$start = memory_get_usage(true);
$tmp = unserialize( serialize( $val ) );
return ( memory_get_usage(true) - $start ) / 1024 / 1024 . " MB";
}
/**
* if is null or
* @param mixed $val
* @return boolean
*/
function isValueless( $val )
{
return is_null($val) || empty($val);
}
function is_valueless($val) { return isValueless($val); }
/**
*
* @param array $array
* @param string $key
* @param string $type
* @return boolean
*/
function checkKey($array, $key, $type){
if( !is_array($array) ) return false;
if( !array_key_exists($key, $array) ) return false;
switch($type){
case 'callback' : return is_callable( $array[$key] );
case 'array' : return is_array( $array[$key] );
case 'string' : return is_string( $array[$key] );
case 'numeric' : return is_numeric( $array[$key] );
case 'null' : return is_null( $array[$key] );
case 'object' : return is_object( $array[$key] );
default : return is_a( $array[$key], $type );
}
}
// http://www.sitecrafting.com/blog/php-getcaller
function getCaller($i=2, $all=false) {
$trace = debug_backtrace();
if( $all ) return $trace[$i];
$name = $trace[$i]['class'] . "::" . $trace[$i]['function'];
return empty($name) ? 'global' : $name;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment