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);
}
#2

[eluser]xwero[/eluser]
I should read more blogs or take the time to remember all the string functions php offers.

The preg_match in helper is replaced by strncasecmp.

Code:
/**
* Array match key - checks if the array key exists using a string
*
* @access    public
* @param    string
* @param    array
* @return    mixed
*/    
function array_key_matches_string($string,$array)
{
    $postkeys = array_keys($array);
    foreach($postkeys as $key)
    {
        if(strncasecmp($string,$key,strlen($string)) == 0)
        {
            return $key;
        }
    }
    return false;
}

now you can do
Code:
$updatematch = array_match_key('update_',$_POST);
if(is_string($updatematch))
{
   $id = str_replace('update_',0,$updatematch);
}

This way it's closer to the native php functions but you lose the flexibility regex offers. I have to look into the performance for the two functions.
#3

[eluser]Pygon[/eluser]
You might use strstr or stristr instead, since strncasecmp is only going to compare the first X characters of the string.
#4

[eluser]xwero[/eluser]
@Pygon: you can do this in one line
Code:
$needle = 'test';
$keys = array_keys($somearray);
if(in_array($needle,$keys)) // here the magic happens
{
   // do something with the array and the key
}
This way you only have to do one function call to get the array keys.

The main reason why i have written the functions is because sometimes i append data to my button names which makes it impossible to check them with php native functions. And because the buttons are created dynamically i can't hardcode the button action.
Code:
&lt;input type="submit" name="update_1" value="Update"&gt;<br>
&lt;input type="submit" name="update_2" value="Update"&gt;
I could do the same as in the first snippet and make the second parameter the keys array which saves me a function call but i know my lazy self and i will end up doing
Code:
array_key_matches_string('update_',array_keys($_POST));
Oh well use it any way you want the functions are up for grabs Smile
#5

[eluser]Pygon[/eluser]
I recognize fully what the goal of the function is.

in_array, array_search and array_keys($array,$search) don't match partial values.

My point was that using strstr or stristr, you can match "update_*" or "*_update", unlike with strncasecmp which could only match the first X chars of the string.

Probably would be almost the same speed and would keep more of the preg_match functionality without the preg_match slow-down.
#6

[eluser]xwero[/eluser]
[quote author="Pygon" date="1203545129"]You might use strstr or stristr instead, since strncasecmp is only going to compare the first X characters of the string.[/quote]
You are right strncasecmp only matches the first characters. it the same as doing
Code:
array_key_matches_regex('/^update_',$_POST);
That's what i meant saying the array_key_matches_string function lacks flexibility.

Because i know the first part strncasecmp does the trick but sometimes i only need the appended data so the strstr suggestion is a nice catch. The function could be written as follows
Code:
function array_key_matches_string($string,$array,$cut=FALSE)
{
    $postkeys = array_keys($array);
    foreach($postkeys as $key)
    {
        if(strncasecmp($string,$key,strlen($string)) == 0)
        {
            return (!$cut)$key:substr($key,strlen($string)+1);
        }
    }
    return false;
}

I ended up with substr because the code with strstr would be
Code:
str_replace($string,'',strstr($key,$string))
Because it returns the needle too.
#7

[eluser]xwero[/eluser]
[quote author="Pygon" date="1203547557"]
My point was that using strstr or stristr, you can match "update_*" or "*_update", unlike with strncasecmp which could only match the first X chars of the string.
[/quote]
I didn't know you could add wildcards to the strstr needle, thanks for pointing that out.
#8

[eluser]Pygon[/eluser]
Sorry for the misunderstanding, my use of the astrisk was simply to denote data other than the specified string.

Code:
strstr("update_","update_1"); // will evaluate == true because it returns "update_1"
strstr("update_","_updated2"); // will evaluate === FALSE
strstr("update_","my_update_3rd"); // will evaluate == true because it returns "update_3rd"

Re-explained:

The only reason I pointed strstr/stristr out was that you could match something pre-pended "update_" or something appended "_update" using the same function.
#9

[eluser]xwero[/eluser]
I was thinking did i overlook something on the php.net function page or is it something you found out by experience.

The only problem with the using strstr is you can't retrieve the value in front of the needle using a php version lower than 6. An than you still have to remove the needle from the result.

If you want the prefix of a key the function will be
Code:
function array_key_matches_rstring($string,$array,$cut=FALSE)
{
    $postkeys = array_keys($array);
    $string = strrev($string);
    foreach($postkeys as $key)
    {
        $key = strrev($key)
        if(strncasecmp($string,$key,strlen($string)) == 0)
        {
            return (!$cut)$key:substr($key,0,strlen($key)-strlen($string));
        }
    }
    return false;
}




Theme © iAndrew 2016 - Forum software by © MyBB