Ignore comments at the end of line

If the variable value had no quotes a comment at the end of the line was
added to the variable.
Spaces between text and comment mark will be removed too

Old:
FOO=Test # Comment -> $_ENV['FOO'] = "Test # Comment"

New:
FOO=Test # Comment -> $_ENV['FOO'] = "Test"
This commit is contained in:
2022-06-09 09:17:21 +09:00
parent 1389d5c768
commit 0d3e10fe26
4 changed files with 47 additions and 2 deletions

View File

@@ -6,6 +6,9 @@ namespace gullevek\dotEnv;
class DotEnv
{
/** @var string constant comment char, set to # */
private const COMMENT_CHAR = '#';
/**
* parses .env file
*
@@ -72,6 +75,11 @@ class DotEnv
// add removed new line back because this is a multi line
$value = ltrim($value, '"') . PHP_EOL;
}
} else {
// strip any quotes at end for unquoted single line
// an right hand spaces are removed too
$value = false !== ($pos = strpos($value, self::COMMENT_CHAR)) ?
rtrim(substr($value, 0, $pos)) : $value;
}
// if block is set, we strip line of slashes
$_ENV[$var] = $block === true ? stripslashes($value) : $value;
@@ -95,5 +103,4 @@ class DotEnv
}
}
// __END__