[eluser]Unknown[/eluser]
Simple functions for helpers, I think very necessary and usually use a lot.
This function, valid the alphanumeric string and return true or false.
Code:
/**
* Valid alphanumeric string
*
*
*
* @access public
* @return bool
*/
if( ! function_exists('alphanumeric'))
{
function alphanumeric($var)
{
return ( ! preg_match("/^([a-zA-Z0-9])+$/i", $var)) ? FALSE : TRUE;
}
}
This function remove all non-alphanumeric characters and return the new string.
Code:
/**
* Remove all non-alphanumeric characters
*
*
*
* @access public
* @return string
*/
if( ! function_exists("remove_non_alphanumeric"))
{
function remove_non_alphanumeric($string)
{
return preg_replace("/[^a-zA-Z0-9\s]/", NULL, $string);
}
}
This function is similar to "array_map", but does not work with object as needed to find another alternative, so I created this function.
Code:
/**
* This function is similar to "array_map" but specifies to object.
*
*
*
* @access public
* @return object
*/
if( ! function_exists("object_map"))
{
function object_map($function,$object)
{
if(!function_exists($function))
{
return exit('Error [object_map]: the function ['.$function.'] not exists');
}
$return = new stdClass();
foreach ($object as $k => $v)
{
if (is_object($v))
{
$return->$k = object_map($function,$v);
}
else
{
$return->$k = $function($v);
}
}
return $return;
}
}
This function generates a key with the size chosen in the first parameter.
Code:
/**
* Simple key generator
*
* This function generates a key with the size chosen in the first parameter.
*
* @access public
* @return string
*/
if( ! function_exists("gerarkey"))
{
function gerarkey($length = 40)
{
$key = NULL;
$pattern = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRTWXYZ';
for( $i = 0; $i < $length; ++$i )
{
$key .= $pattern{rand(0,58)};
}
return $key;
}
}
}
Convert bytes to kb, mb, gb, tb and pb
Code:
/**
* Convert bytes
*
* Convert bytes to kb, mb, gb, tb and pb
*
* @access public
* @return string
*/
if( ! function_exists("convert_size"))
{
function convert_size($size, $uppercase = false)
{
if(!$uppercase)
{
$unit = array('b','kb','mb','gb','tb','pb');
} else
{
$unit = array('B','KB','MB','GB','TB','PB');
}
return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i];
}
}