Last active
November 17, 2016 04:02
-
-
Save FirePanther/400d57a37db5b83ca978 to your computer and use it in GitHub Desktop.
Parse Interface File for Linux, written for @patschi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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