Guest User

Untitled

a guest
Jul 23rd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. $lines = explode("n", $configText);
  2. $tree = [];
  3. $currentKey = null;
  4.  
  5. foreach ($lines as $row) {
  6. if (empty($row)) {
  7. continue;
  8. } elseif (preg_match('/^s*[(.*?)]s*$/', $row, $matches)) {
  9. $currentKey = trim($matches[1]);
  10. } elseif ($currentKey && preg_match('/^(w+)=(.*)/', $row, $matches)) {
  11. $currentProperty = trim($matches[1]);
  12. $currentValues = trim($matches[2]);
  13. $values = [];
  14.  
  15. if (preg_match_all('/(w+)=/', $currentValues, $matches)) {
  16. $values = preg_split('/(w+)=/', $currentValues);
  17. $values = array_map('trim', $values);
  18.  
  19. if (empty($values[0])) {
  20. unset($values[0]);
  21. } else {
  22. array_unshift($matches[1], "");
  23. }
  24.  
  25. $values = array_combine($matches[1], $values);
  26. } else {
  27. $values[""] = $currentValues;
  28. }
  29.  
  30. $tree[$currentKey][$currentProperty] = [
  31. 'value' => $currentValues,
  32. 'params' => $values,
  33. ];
  34. } else {
  35. throw new Exception('Unexpected scenario');
  36. }
  37. }
  38.  
  39. print_r($tree);
Add Comment
Please, Sign In to add comment