Mistři v programování na #hovnokod

od Romana

    <?
     
     
    $post = array();
    $post['blaf'] = "ondra smrdí";
    $post['yeah']['yeah'] = "Kcko je best";
    $post['test']['var1']['var2'] = "netolicak smrdí";
     
     
    echo "<h2>Projde</h2>";
     
    echo vasikovaMagie('blaf', $post) . "<hr />";
    echo vasikovaMagie('yeah[yeah2]', $post) . "<hr />";
    echo vasikovaMagie('test[var1][var2]', $post);
     
     
    echo "<h2>Neprojde - špatné indexy - schválně</h2>";
     
    echo vasikovaMagie('blaF', $post) . "<hr />";
    echo vasikovaMagie('yeah999[yeah2]', $post) . "<hr />";
    echo vasikovaMagie('test[var1][var2454]', $post);
     
     
    function vasikovaMagie($str, $post)
    {
        // pouze 1 hodnota
        if (strpos($str, '[') === FALSE)
        {
            return (isset($post[$str]) ? $post[$str] : 'Bad index - "' . $str.'"');
        }
        else
        {
            $indexes = array();
            $indexes[0] = preg_replace('~([^[]+)\[.+~', "$1", $str);
           
            preg_match_all('~\[.+?\]~', $str, $m);
           
            foreach ($m[0] as $index)
            {
                $indexes[] = strtr($index, array('[' => '', ']' => ''));
            }
           
            $total = count($indexes);
       
            $showArgs = function($indexes)
            {  
                return implode(", ", $indexes);
            };
           
            switch ($total)
            {
                case 2:
                return (isset($post[$indexes[0]][$indexes[1]]) ? $post[$indexes[0]][$indexes[1]] : 'Bad any indexes - "' . $showArgs($indexes).'"');
                break;
               
                case 3:
                return (isset($post[$indexes[0]][$indexes[1]][$indexes[2]]) ? $post[$indexes[0]][$indexes[1]][$indexes[2]] : 'Bad any indexes - "' . $showArgs($indexes).'"');
                break;
               
                case 4:
                return (isset($post[$indexes[0]][$indexes[1]][$indexes[2]][$indexes[3]]) ? $post[$indexes[0]][$indexes[1]][$indexes[2]][$indexes[3]] : 'Bad any indexes - "' . $showArgs($indexes).'"');
                break;
               
                case  ($total >= 1):
                    Throw new Exception('Undefined number of arguments, greater or equal five!');
                break;
            }
           
        }
       
    }

Anonymous,