SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    982
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Wildcard match on array keys

    Curious if anyone has a cool snippet or idea for how to accomplish the following:
    Code:
    /**
     *  Select all of the keys matching a given key.  Asterisks (*)
     *      should be treated as wildcards and would match any string
     *      similar to a regex expression: 'test_*_asdf' => 'test_.+_asdf'
     *
     *  @param string $wild_key
     *  @param array  $array
     *  @return array
     */
    array_key_exists_wildcard($wild_key, $array);
    Obviously it would be nice to just replace * with .+, but special characters could cause a problem, right? I also couldn't come up with a graceful solution using explode.
    MySQL v5.1.58
    PHP v5.3.6

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    function array_key_exists_wildcard $arr$nee )
    {
        
    $nee str_replace'*''###star_needle###'$nee );
        
    $nee preg_quote$nee'/' ); # This is important!
        
    $nee str_replace'###star_needle###''.*?'$nee );
        
    $nee '/^' $nee '$/i';

        return 
    preg_grep$neearray_keys$arr ) );
    }

    $arr = array(
        
    'file_1224rf_meh.jpg' => 111,
        
    'file_506640_jones.jpg' => 555
    );

    var_dumparray_key_exists_wildcard$arr'file_*_jones.jpg' ) ); 
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    My Attempt.
    PHP Code:
    <?php
    /**
     * @desc    ...
     *
     * @param    String    $sWildKey
     * @param    Array    $aArray
     * @return    Array
     */
    function array_key_exists_wildcard($sWildKey$aArray)
    {
        
    $sPattern sprintf(
            
    '~%s~',
            
    str_replace('\*''(.{1})'preg_quote($sWildKey))
        );
        
    $aFinal = array();
        foreach (
    $aArray as $mKey => $mValue)
        {
            if(
    preg_match($sPattern$mKey))
            {
                
    $aFinal[$mKey] = $mValue;
            }
        }
        return 
    $aFinal;
    }

    $aArray = array(
        
    'Foo'    =>    'Bar',
        
    'Bar'    =>    'Foo'
    );

    print_r(array_key_exists_wildcard('F*'$aArray));
    /*
    Array
    (
        [Foo] => Bar
    )
    */

    print_r(array_key_exists_wildcard('Ba*'$aArray));
    /*
    Array
    (
        [Bar] => Foo
    )
    */
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  4. #4
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    982
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    logic, you continue to amaze me preg_quote is really good to know; thanks for the tip.

    Thanks for the attempt too Silver.
    MySQL v5.1.58
    PHP v5.3.6

  5. #5
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Could simplify it of course...It only returns the keys (case-insenitive...) that match.
    PHP Code:
    function array_key_exists_wildcard $arr$nee )
    {
        
    $nee str_replace'\*''.*?'preg_quote$nee'/' ) );
        return 
    preg_grep'/^' $nee '$/i'array_keys$arr ) );

    If you want the keys and values:
    PHP Code:
    function array_key_exists_wildcard $arr$nee )
    {
        
    $nee str_replace'\*''.*?'preg_quote$nee'/' ) );
        
    $nee preg_grep'/^' $nee '$/i'array_keys$arr ) );
        return 
    array_intersect_key$arrarray_flip$nee ) );

    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •