1 common.inc | backdrop_parse_info_format($data, $process_sections = FALSE) |
Parses data in Backdrop's .info format.
Data should be in an .ini-like format to specify values. White-space generally doesn't matter, except inside values:
key = value
key = "value"
key = 'value'
key = "multi-line
value"
key = 'multi-line
value'
key
=
'value'
Arrays are created using a HTTP GET alike syntax:
key[] = "numeric array"
key[index] = "associative array"
key[index][] = "nested numeric array"
key[index][index] = "nested associative array"
PHP constants are substituted in, but only when used as the entire value. Comments should start with a semi-colon at the beginning of a line.
Parameters
string $data: A string to parse.
bool $process_sections: Flag indicating if sections should be parsed.
Return value
The info array.:
See also
File
- core/
includes/ common.inc, line 8579 - Common functions that many Backdrop modules will need to reference.
Code
function backdrop_parse_info_format($data, $process_sections = FALSE) {
$info = array();
// Separate by sections and parse individually if requested.
if ($process_sections) {
// Remove leading a trailing comments before groups.
$data = trim(preg_replace('/;.*$/m', '', $data));
// Split the content by the groups present.
$split = preg_split('/^\[(.*)\]\s*$/m', $data, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
$split_count = count($split);
// If sections are found, parse each individually and add to the result.
if ($split_count > 1) {
$info_sections = array();
for ($n = 0; $n < $split_count; $n = $n + 2) {
$info_sections[$split[$n]] = backdrop_parse_info_format($split[$n + 1], FALSE);
}
return $info_sections;
}
}
if (preg_match_all('
@^\s* # Start at the beginning of a line, ignoring leading whitespace
((?:
[^=;\[\]]| # Key names cannot contain equal signs, semi-colons or square brackets,
\[[^\[\]]*\] # unless they are balanced and not nested
)+?)
\s*=\s* # Key/value pairs are separated by equal signs (ignoring white-space)
(?:
("(?:[^"]|(?<=\\\\)")*")| # Double-quoted string, which may contain slash-escaped quotes/slashes
(\'(?:[^\']|(?<=\\\\)\')*\')| # Single-quoted string, which may contain slash-escaped quotes/slashes
([^\r\n]*?) # Non-quoted string
)\s*$ # Stop at the next end of a line, ignoring trailing whitespace
@msx', $data, $matches, PREG_SET_ORDER)) {
foreach ($matches as $match) {
// Fetch the key and value string.
$i = 0;
$parts = array();
foreach (array('key', 'value1', 'value2', 'value3') as $var) {
$parts[$var] = isset($match[++$i]) ? $match[$i] : '';
}
$value = stripslashes(substr($parts['value1'], 1, -1)) . stripslashes(substr($parts['value2'], 1, -1)) . $parts['value3'];
// Parse array syntax.
$keys = preg_split('/\]?\[/', rtrim($parts['key'], ']'));
$last = array_pop($keys);
$parent = &$info;
// Create nested arrays.
foreach ($keys as $key) {
if ($key == '') {
$key = count($parent);
}
if (!isset($parent[$key]) || !is_array($parent[$key])) {
$parent[$key] = array();
}
$parent = &$parent[$key];
}
// Handle PHP constants.
if (preg_match('/^\w+$/', $value) && defined($value)) {
$value = constant($value);
}
// Insert actual value.
if ($last == '') {
$last = count($parent);
}
$parent[$last] = $value;
}
}
return $info;
}