From 75367b27249a1f9fb56ea27b3e1e184b4f1e3f7f Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Mon, 11 Oct 2021 09:30:36 +0900 Subject: [PATCH] Fix next line read with double quote blocks in one line If there was this FOO="bar" Then the # comment block was attached to bar because FOO was seen as a multiline block although the quotes are in one line only --- src/read_env_file.php | 3 ++- test/.env | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/read_env_file.php b/src/read_env_file.php index 6176ec8..400ef7e 100644 --- a/src/read_env_file.php +++ b/src/read_env_file.php @@ -49,11 +49,12 @@ function readEnvFile(string $path = __DIR__, string $env_file = '.env'): int // wirte only if env is not set yet, and write only the first time if (empty($_ENV[$var])) { if (!empty($quotes)) { - $block = true; // match greedy for first to last so we move any " if there are if (preg_match('/^"(.*[^\\\])"/U', $value, $matches)) { $value = $matches[1]; } else { + // this is a multi line + $block = true; // first " in string remove // add removed new line back because this is a multi line $value = ltrim($value, '"') . PHP_EOL; diff --git a/test/.env b/test/.env index cca580b..4c97044 100644 --- a/test/.env +++ b/test/.env @@ -2,6 +2,7 @@ SOMETHING=A OTHER="B IS B" Complex="A B \"D is F" +# COMMENT HAS_SPACE= "ABC"; FAILURE = ABC SIMPLEBOX= A B C