tristankechlo / simple-dotenv
simple library to parse .env files
Installs: 22
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/tristankechlo/simple-dotenv
Requires
- php: >=8.0
Requires (Dev)
- phpunit/phpunit: ^9.6
README
small parser for .env files
normal parsing
# .env file KEY1="This is a string" KEY2=234 KEY3=4.5 KEY4=TRUE KEY5=OFF
# php file use TK\Dotenv\Dotenv; $content = file_get_contents("./.env"); $actual = Dotenv::parse($content);
# output $actual = [ "KEY1" => "This is a string", "KEY2" => "234", "KEY3" => "4.5", "KEY4" => "TRUE", "KEY5" => "OFF", ];
parsing with conversion
# .env file KEY1="This is a string" KEY2=234 KEY3=4.5 KEY4=TRUE KEY5=OFF
# php file use TK\Dotenv\Dotenv; $content = file_get_contents("./.env"); $actual = Dotenv::parse($content, true);
# output $actual = [ "KEY1" => "This is a string", "KEY2" => 234, "KEY3" => 4.5, "KEY4" => true, "KEY5" => false, ];
conversion
The parse can convert some values to php primitive types, otherwise all values are string.
booleans
these values will be converted to booleans (case insensitive):
- truthy_values = ['true', 'yes', 'on']
- falsy_values = ['false', 'no', 'off']
numbers
these values will be converted to numbers:
- integers (e.g.
3or23424) - floats (e.g.
5.76or43234.3453) - hexadezimal only with prefix
0x(e.g.0xa0to160) - binary only with prefix
0b(e.g.0b110011to51) - octal only with prefix
0o(e.g.0o77to63)
null
these values will be converted to null (case insensitive):
- null
- none