-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathParser.php
More file actions
38 lines (30 loc) · 971 Bytes
/
Copy pathParser.php
File metadata and controls
38 lines (30 loc) · 971 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php declare(strict_types = 1);
namespace SecureEnvPHP;
class Parser
{
/**
* Parses environment file variables and returns an associative array
*
* @param string $content
*
* @return array
*/
public static function parse(string $content): array
{
$lines = explode("\n", $content);
$object = [];
foreach ($lines as $line) {
if (preg_match('/^\s*([\w\.\-]+)\s*=\s*(.*)?\s*$/', $line, $matches)) {
$key = $matches[1];
$value = $matches[2] ?? '';
$length = $value ? strlen($value) : 0;
if ($length > 0 && strpos($value, '"') === 0 && substr($value, -1) === '"') {
$value = preg_replace('/\\n/gm', "\n", $value);
}
$value = trim(preg_replace('/(^[\'"]|[\'"]$)/', '', $value));
$object[$key] = $value;
}
}
return $object;
}
}