- var_export: documentation ( source)
- explode: documentation ( source)
- parse_ini_string: documentation ( source)
- key: documentation ( source)
- key_exists: documentation ( source)
<?php
$ini = <<<INI
TPL_CONTACT_MISC="Miscellaneous Info"
date.timezone = Pacific/Tahiti
TPL_CONTACT_MISC="Your german misc info translation"
COM_TODO_ALIGN="Image Float"
JOOMLA! DEFAULT IMAGES=Жуумла! үндсэн зурагнууд
COM_TODO_ALIGN_DESC="This will apply the classes 'pull-left', 'pull-center' or 'pull-right' to the '<figure>' or '<img>' element"
COM_TODO_BROWSE_FILES="Browse files"
COM_TODO_CAPTION="Caption"
COM_TODO_CAPTION_CLASS_LABEL="Caption Class"
COM_MEMBERSHIPS_FIELD_MEMBERSHIP_LABEL 911
COM_FRED_GREETING='Hello'
COM_FRED_GREETING_BARNEY='Yo, Buddy'
DR_SOLD="SOLD"
upload_tmp_dir = /var/www/html/tmp
INI;
$result = [];
foreach (explode(PHP_EOL, $ini) as $i => $line) {
$parsed = @parse_ini_string($line);
if (!is_array($parsed)) {
$anomalies[] = "Invalid character(s) @ line index $i: $line";
continue;
}
$key = key($parsed);
if ($key === null) {
$anomalies[] = "No valid declaration @ line index $i: $line";
} elseif (key_exists($key, $result)) {
$anomalies[] = "Redundant key declaration @ line index $i: $key => $parsed[$key]";
} else {
$result[$key] = $parsed[$key];
}
}
var_export($anomalies);
echo "\n---\n";
var_export($result);