Skip to content

Instantly share code, notes, and snippets.

@FirePanther
Last active November 17, 2016 04:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FirePanther/400d57a37db5b83ca978 to your computer and use it in GitHub Desktop.
Save FirePanther/400d57a37db5b83ca978 to your computer and use it in GitHub Desktop.
Parse Interface File for Linux, written for @patschi
<?php
function parse_interface($data) {
// remove comments
$data = preg_replace('~\#.*$~m', "\n", $data);
preg_match_all('~\nauto\s+(\w+)\niface \1 inet (\w+)\n([ \t]+.+\n)*~', $data, $m, PREG_SET_ORDER);
$result = [];
foreach ($m as $v) {
$iface = [];
$iface['auto'] = $v[1];
$iface['inet'] = $v[2];
if (isset($v[3])) $iface['params'] = parse_params($v[0]);
$result[] = $iface;
}
return $result;
}
function parse_params($params) {
$rparams = [];
preg_match_all('~^\s+([\w-]+)\s+(.+)$~m', trim($params), $m, PREG_SET_ORDER);
foreach ($m as $k) {
$rparams[$k[1]] = $k[2];
}
return $rparams;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment