Welcome Guest, Not a member yet? Register   Sign In
extended array helper functions : array_key_matches_regex and array_key_matches_string
#1

[eluser]xwero[/eluser]
Because the array_key_exists doesn't support regular expressions i've made a function that does.
Code:
/**
* Array match key - checks if the array key exists using a regex
*
* @access    public
* @param    string
* @param    array
* @return    mixed
*/    
function array_key_matches_regex($regex,$array)
{
    $postkeys = array_keys($array);
    foreach($postkeys as $key)
    {
        if(preg_match($regex,$key))
        {
            return $key;
        }
    }
    return false;
}

It's useful for getting form buttons that have data added to their name.
Code:
$updatematch = array_match_key('/^update_/',$_POST);
if(is_string($updatematch))
{
   $id = str_replace('update_',0,$updatematch);
}


Messages In This Thread
extended array helper functions : array_key_matches_regex and array_key_matches_string - by El Forum - 02-12-2008, 02:48 AM



Theme © iAndrew 2016 - Forum software by © MyBB