यह propably एक छोटे से देर से आता है, लेकिन काम करता है जिस तरह से parse_ini_file PHPs परेशान समस्या से छुटकारा पाने के लिए, मैं दोहरे उद्धरण में स्ट्रिंग लिपटे मुझे इतना इतना है कि मैंने अपना खुद का छोटा पार्सर लिखा था।
इसका उपयोग करने के लिए स्वतंत्र महसूस करें, लेकिन देखभाल के साथ इसका उपयोग केवल उथल-पुथल परीक्षण किया गया है!
// the exception used by the parser
class IniParserException extends \Exception {
public function __construct($message, $code = 0, \Exception $previous = null) {
parent::__construct($message, $code, $previous);
}
public function __toString() {
return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
}
}
// the parser
function my_parse_ini_file($filename, $processSections = false) {
$initext = file_get_contents($filename);
$ret = [];
$section = null;
$lineNum = 0;
$lines = explode("\n", str_replace("\r\n", "\n", $initext));
foreach($lines as $line) {
++$lineNum;
$line = trim(preg_replace('/[;#].*/', '', $line));
if(strlen($line) === 0) {
continue;
}
if($processSections && $line{0} === '[' && $line{strlen($line)-1} === ']') {
// section header
$section = trim(substr($line, 1, -1));
} else {
$eqIndex = strpos($line, '=');
if($eqIndex !== false) {
$key = trim(substr($line, 0, $eqIndex));
$matches = [];
preg_match('/(?<name>\w+)(?<index>\[\w*\])?/', $key, $matches);
if(!array_key_exists('name', $matches)) {
throw new IniParserException("Variable name must not be empty! In file \"$filename\" in line $lineNum.");
}
$keyName = $matches['name'];
if(array_key_exists('index', $matches)) {
$isArray = true;
$arrayIndex = trim($matches['index']);
if(strlen($arrayIndex) == 0) {
$arrayIndex = null;
}
} else {
$isArray = false;
$arrayIndex = null;
}
$value = trim(substr($line, $eqIndex+1));
if($value{0} === '"' && $value{strlen($value)-1} === '"') {
// too lazy to check for multiple closing " let's assume it's fine
$value = str_replace('\\"', '"', substr($value, 1, -1));
} else {
// special value
switch(strtolower($value)) {
case 'yes':
case 'true':
case 'on':
$value = true;
break;
case 'no':
case 'false':
case 'off':
$value = false;
break;
case 'null':
case 'none':
$value = null;
break;
default:
if(is_numeric($value)) {
$value = $value + 0; // make it an int/float
} else {
throw new IniParserException("\"$value\" is not a valid value! In file \"$filename\" in line $lineNum.");
}
}
}
if($section !== null) {
if($isArray) {
if(!array_key_exists($keyName, $ret[$section])) {
$ret[$section][$keyName] = [];
}
if($arrayIndex === null) {
$ret[$section][$keyName][] = $value;
} else {
$ret[$section][$keyName][$arrayIndex] = $value;
}
} else {
$ret[$section][$keyName] = $value;
}
} else {
if($isArray) {
if(!array_key_exists($keyName, $ret)) {
$ret[$keyName] = [];
}
if($arrayIndex === null) {
$ret[$keyName][] = $value;
} else {
$ret[$keyName][$arrayIndex] = $value;
}
} else {
$ret[$keyName] = $value;
}
}
}
}
}
return $ret;
}
इसे दूसरे तरीके से करता है क्या? परिवर्तनीय नामों में केवल अल्फान्यूमेरिकल वर्ण हो सकते हैं लेकिन इसके अलावा उनके लिए कोई प्रतिबंध नहीं है। तार के साथ "सब कुछ no
, yes
, true
, false
, on
, off
, null
या none
की तरह एक विशेष मूल्य हो गया है समझाया जाना चाहिए। मानचित्रण के लिए कोड देखें।
+1 ओह, अच्छा कामकाज :-) – lonesomeday
धन्यवाद, शायद यह करने का एकमात्र तरीका है। –
यदि आपके मान अद्वितीय नहीं हैं तो यह तोड़ नहीं होगा? वे सभी * मूल्य * के बाद हैं, कुंजी नहीं। – Pacerier