Advertisement
villers

Untitled

May 1st, 2014
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.12 KB | None | 0 0
  1. #!/usr/bin/php
  2. <?php
  3.  
  4. class System
  5. {
  6.     function my_getopt()
  7.     {
  8.         global $argv;
  9.         $result = array();
  10.  
  11.         $current_key = false;
  12.         foreach($argv as $opt)
  13.         {
  14.             $matches = array();
  15.             if(preg_match("/^-([a-zA-Z]*)/", $opt, $matches))
  16.             {
  17.                 $current_key = $matches[1];
  18.  
  19.                 if(!isset($result[$current_key]))
  20.                 {
  21.                     $result[$current_key] = false;
  22.                 }
  23.             }
  24.             else if (false != $current_key)
  25.             {
  26.  
  27.                 if(false == $result[$current_key])
  28.                 {
  29.                     $result[$current_key] = $opt;
  30.                 }
  31.                 else
  32.                 {
  33.                     if(false == is_array($result[$current_key]))
  34.                     {
  35.                         $result[$current_key] = array($result[$current_key]);
  36.                     }
  37.                     $result[$current_key][] = $opt;
  38.                 }
  39.             }
  40.         }
  41.         return $result;
  42.     }
  43. }
  44.  
  45. $system = new system;
  46. print_r($system->my_getopt());
  47.  
  48. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement