Guest User

Bibek Paudel

a guest
Jun 3rd, 2009
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.83 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  
  5. Code courtesy of: arnapou, taken from http://www.php.net/manual/en/function.parse-ini-file.php#78221
  6.  
  7. Read file : $ini = INI::read('myfile.ini');
  8. Write file : INI::write('myfile.ini', $ini);
  9.  
  10. Features :
  11. - support [] syntax for arrays
  12. - support . in keys like bar.foo.something = value
  13. - true and false string are automatically converted in booleans
  14. - integers strings are automatically converted in integers
  15. - keys are sorted when writing
  16. - constants are replaced but they should be written in the ini file between braces : {MYCONSTANT}
  17.  
  18. */
  19.  
  20. class INI {
  21.     /**
  22.      *  WRITE
  23.      */
  24.     static function write($filename, $ini) {
  25.         $string = '';
  26.         foreach(array_keys($ini) as $key) {
  27.             $string .= '['.$key."]\n";
  28.             $string .= INI::write_get_string($ini[$key], '')."\n";
  29.         }
  30.         file_put_contents($filename, $string);
  31.     }
  32.     /**
  33.      *  write get string
  34.      */
  35.     static function write_get_string(& $ini, $prefix) {
  36.         $string = '';
  37.         ksort($ini);
  38.         foreach($ini as $key => $val) {
  39.             if (is_array($val)) {
  40.                 $string .= INI::write_get_string($ini[$key], $prefix.$key.'.');
  41.             } else {
  42.                 $string .= $prefix.$key.' = '.str_replace("\n", "\\\n", INI::set_value($val))."\n";
  43.             }
  44.         }
  45.         return $string;
  46.     }
  47.     /**
  48.      *  manage keys
  49.      */
  50.     static function set_value($val) {
  51.         if ($val === true) { return 'true'; }
  52.         else if ($val === false) { return 'false'; }
  53.         return $val;
  54.     }
  55.     /**
  56.      *  READ
  57.      */
  58.     static function read($filename) {
  59.         $ini = array();
  60.         $lines = file($filename);
  61.         $section = 'default';
  62.         $multi = '';
  63.         foreach($lines as $line) {
  64.             if (substr($line, 0, 1) !== ';') {
  65.                 $line = str_replace("\r", "", str_replace("\n", "", $line));
  66.                 if (preg_match('/^\[(.*)\]/', $line, $m)) {
  67.                     $section = $m[1];
  68.                 } else if ($multi === '' && preg_match('/^([a-z0-9_.\[\]-]+)\s*=\s*(.*)$/i', $line, $m)) {
  69.                     $key = $m[1];
  70.                     $val = $m[2];
  71.            
  72.            
  73.             @@//to remove the commented part starting from # at positions other than beginning of the line
  74.             @@//added by bibekpaudel
  75.  
  76.             $hash_pos=strpos($val, "#");
  77.             $val=substr($val, 0, $hash_pos);
  78.  
  79.             //additional code ends
  80.  
  81.                     if (substr($val, -1) !== "\\") {
  82.                         $val = trim($val);
  83.                         INI::manage_keys($ini[$section], $key, $val);
  84.                         $multi = '';
  85.                     } else {
  86.                         $multi = substr($val, 0, -1)."\n";
  87.                     }
  88.                 } else if ($multi !== '') {
  89.                     if (substr($line, -1) === "\\") {
  90.                         $multi .= substr($line, 0, -1)."\n";
  91.                     } else {
  92.                         INI::manage_keys($ini[$section], $key, $multi.$line);
  93.                         $multi = '';
  94.                     }
  95.                 }
  96.             }
  97.         }
  98.      
  99.         $buf = get_defined_constants(true);
  100.         $consts = array();
  101.         foreach($buf['user'] as $key => $val) {
  102.             $consts['{'.$key.'}'] = $val;
  103.         }
  104.         array_walk_recursive($ini, array('INI', 'replace_consts'), $consts);
  105.         return $ini;
  106.     }
  107.     /**
  108.      *  manage keys
  109.      */
  110.     static function get_value($val) {
  111.         if (preg_match('/^-?[0-9]$/i', $val)) { return intval($val); }
  112.         else if (strtolower($val) === 'true') { return true; }
  113.         else if (strtolower($val) === 'false') { return false; }
  114.         else if (preg_match('/^"(.*)"$/i', $val, $m)) { return $m[1]; }
  115.         else if (preg_match('/^\'(.*)\'$/i', $val, $m)) { return $m[1]; }
  116.         return $val;
  117.     }
  118.     /**
  119.      *  manage keys
  120.      */
  121.     static function get_key($val) {
  122.         if (preg_match('/^[0-9]$/i', $val)) { return intval($val); }
  123.         return $val;
  124.     }
  125.     /**
  126.      *  manage keys
  127.      */
  128.     static function manage_keys(& $ini, $key, $val) {
  129.         if (preg_match('/^([a-z0-9_-]+)\.(.*)$/i', $key, $m)) {
  130.             INI::manage_keys($ini[$m[1]], $m[2], $val);
  131.         } else if (preg_match('/^([a-z0-9_-]+)\[(.*)\]$/i', $key, $m)) {
  132.             if ($m[2] !== '') {
  133.                 $ini[$m[1]][INI::get_key($m[2])] = INI::get_value($val);
  134.             } else {
  135.                 $ini[$m[1]][] = INI::get_value($val);
  136.             }
  137.         } else {
  138.             $ini[INI::get_key($key)] = INI::get_value($val);
  139.         }
  140.     }
  141.     /**
  142.      *  replace utility
  143.      */
  144.     static function replace_consts(& $item, $key, $consts) {
  145.         if (is_string($item)) {
  146.             $item = strtr($item, $consts);
  147.         }
  148.     }
  149. }
  150.  
  151. ?>
  152.  
Advertisement
Add Comment
Please, Sign In to add comment