Pages

Thursday, August 26, 2010

Utility Functions

Here is the list of some of the important Utility functions.

I have this function as a member function in my Utility Class.


function mb_unserialize($serial_str) {
$out = preg_replace('!s:(\d+):"(.*?)";!se', "'s:'.strlen('$2').':\"$2\";'", $serial_str );
return unserialize($out);
}


function getValidFileName($str)
{
return preg_replace('/[^0-9a-z?-????\`\~\!\@\#\$\%\^\*\(\)\; \,\.\'\/\_\-]/i', ' ',$str);
}


function XML2Array ( $xml , $recursive = false )
{
if ( ! $recursive )
{
$array = simplexml_load_string ( $xml ) ;
}
else
{
$array = $xml ;
}

$newArray = array () ;
$array = ( array ) $array ;
foreach ( $array as $key => $value )
{
$value = ( array ) $value ;
if ( isset ( $value [ 0 ] ) )
{
$newArray [ $key ] = trim ( $value [ 0 ] ) ;
}
else
{
$newArray [ $key ] = XML2Array ( $value , true ) ;
}
}
return $newArray ;
}


function set_flash($text,$type) {
$flash['type'] = $type;
$flash['text'] = $text;
$_SESSION['flash'] = serialize($flash);
}

function get_flash() {
if ($flash = $_SESSION['flash']) {
$_SESSION['flash'] = array();
return unserialize($flash);
} else {
return "";
}
}


function smartslashes($text) {
$magic_quotes_gpc = (bool) ini_get('magic_quotes_gpc');
if (!$magic_quotes_gpc) {
return addslashes($text);
} else {
return $text;
}
}

function check_email_address($email_address)
{
//returns 1 if valid email address (only numeric string), 0 if not
if (eregi("^[\+_a-z0-9-]+(\.[\+_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email_address))
return 1;
else
return 0;
}

No comments:

Post a Comment