note 111845 added to function.parse-ini-string

From: Date: Thu, 04 Apr 2013 07:51:59 +0000
Subject: note 111845 added to function.parse-ini-string
Groups: php.notes php.notes 
Request: Send a blank email to php-notes+get-194331@lists.php.net to get a copy of this message
parse_ini_string_m is analog for a parse_ini_string function. had to code this function due to the lack of a php 5.3 on some hosting. parse_ini_string_m: - ignores commented lines that start with ";" or "#" - ignores broken lines that do not have "=" - supports array values and array value keys <?php function parse_ini_string_m($str) { if(empty($str)) return false; $lines = explode("\n", $str); $ret = Array(); $inside_section = false; foreach($lines as $line) { $line = trim($line); if(!$line || $line[0] == "#" || $line[0] == ";") continue; if($line[0] == "[" &amp;&amp; $endIdx = strpos($line, "]")){ $inside_section = substr($line, 1, $endIdx-1); continue; } if(!strpos($line, '=')) continue; $tmp = explode("=", $line, 2); if($inside_section) { $key = rtrim($tmp[0]); $value = ltrim($tmp[1]); if(preg_match("/^\".*\"$/", $value) || preg_match("/^'.*'$/", $value)) { $value = mb_substr($value, 1, mb_strlen($value) - 2); } $t = preg_match("^\[(.*?)\]^", $key, $matches); if(!empty($matches) &amp;&amp; isset($matches[0])) { $arr_name = preg_replace('#\[(.*?)\]#is', '', $key); if(!isset($ret[$inside_section][$arr_name]) || !is_array($ret[$inside_section][$arr_name])) { $ret[$inside_section][$arr_name] = array(); } if(isset($matches[1]) &amp;&amp; !empty($matches[1])) { $ret[$inside_section][$arr_name][$matches[1]] = $value; } else { $ret[$inside_section][$arr_name][] = $value; } } else { $ret[$inside_section][trim($tmp[0])] = $value; } } else { $ret[trim($tmp[0])] = ltrim($tmp[1]); } } return $ret; } ?> example usage: <?php $ini = ' [simple] val_one = "some value" val_two = 567 [array] val_arr[] = "arr_elem_one" val_arr[] = "arr_elem_two" val_arr[] = "arr_elem_three" [array_keys] val_arr_two[6] = "key_6" val_arr_two[some_key] = "some_key_value" '; $arr = parse_ini_string_m($ini); ?> variable $arr output: Array ( [simple] => Array ( [val_one] => some value [val_two] => 567 ) [array] => Array ( [val_arr] => Array ( [0] => arr_elem_one [1] => arr_elem_two [2] => arr_elem_three ) ) [array_keys] => Array ( [val_arr_two] => Array ( [6] => key_6 [some_key] => some_key_value ) ) ) ---- Server IP: 69.147.83.199 Probable Submitter: 217.20.167.204 ---- Manual Page -- http://php.net/manual/en/function.parse-ini-string.php Edit -- https://master.php.net/note/edit/111845 Del: integrated -- https://master.php.net/note/delete/111845/integrated Del: useless -- https://master.php.net/note/delete/111845/useless Del: bad code -- https://master.php.net/note/delete/111845/bad+code Del: spam -- https://master.php.net/note/delete/111845/spam Del: non-english -- https://master.php.net/note/delete/111845/non-english Del: in docs -- https://master.php.net/note/delete/111845/in+docs Del: other reasons-- https://master.php.net/note/delete/111845 Reject -- https://master.php.net/note/reject/111845 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#194331) next »