How to Parse .env File Contents into an Array in PHP
When building web applications, it is often necessary to store configuration variables such as database credentials, API keys, and other environment-specific settings. One common way to store these variables is by using a .env
file, which contains key-value pairs in a specific format. In this blog post, we will show you how to parse .env
file contents into an array in PHP.
Parsing .env
File Contents
To parse .env
file contents, we need to read the file contents and extract the key-value pairs. Here’s a simple PHP function that can parse .env
file contents and return an array:
function parse_env_file_contents_to_array($env_file_contents) {
$env_array = array();
$lines = explode("\n", $env_file_contents);
foreach ($lines as $line) {
if ($line === '' || substr($line, 0, 1) === '#') {
continue;
}
$equals_pos = strpos($line, '=');
if ($equals_pos !== false) {
$key = substr($line, 0, $equals_pos);
$value = substr($line, $equals_pos + 1);
if (substr($value, 0, 1) === '"' && substr($value, -1) === '"') {
$value = substr($value, 1, -1);
}
elseif (substr($value, 0, 1) === "'" && substr($value, -1) === "'") {
$value = substr($value, 1, -1);
}
$env_array[$key] = $value;
}
}
return $env_array;
}
This function takes in the contents of a .env
file as a string and returns an array of key-value pairs. Let’s break down how this function works.
First, we create an empty array $env_array
to store the key-value pairs. Then, we split the file contents into an array of lines using explode()
. We loop through each line, ignoring blank lines and lines that start with #
(which are comments).
For each non-comment line, we look for the first occurrence of the =
character using strpos()
. If we find the =
character, we extract the key and value of the key-value pair. If the value is enclosed in quotes (either single or double quotes), we remove the quotes.
Finally, we add the key-value pair to the $env_array
and return the resulting array.
Using the parse_env_file_contents_to_array() function
To use the parse_env_file_contents_to_array()
function, we first need to read the contents of the .env
file. We can do this using the file_get_contents()
function, which reads the contents of a file into a string.
Here’s an example of how to use the parse_env_file_contents_to_array()
function:
$env_file_path = '/path/to/.env';
$env_file_contents = file_get_contents($env_file_path);
$env_array = parse_env_file_contents_to_array($env_file_contents);
print_r($env_array);
In this example, we read the contents of the .env
file at /path/to/.env
using file_get_contents()
. We then pass the file contents to the parse_env_file_contents_to_array()
function, which returns an array of key-value pairs. Finally, we use print_r()
to print the resulting array.
Conclusion
In this blog post, we showed you how to parse .env
file contents
into an array in PHP. By using the parse_env_file_contents_to_array()
function, we can easily read and use the key-value pairs stored in a .env
file in our PHP applications.
It’s worth noting that the parse_env_file_contents_to_array()
function we provided is a simple implementation that assumes the key-value pairs are in a specific format. If your .env
file uses a different format or contains complex values, you may need to modify the function to suit your needs.
We hope this blog post was helpful in understanding how to parse .env
file contents into an array in PHP. Here’s the complete code for the parse_env_file_contents_to_array()
function:
function parse_env_file_contents_to_array($env_file_contents) {
$env_array = array();
$lines = explode("\n", $env_file_contents);
foreach ($lines as $line) {
if ($line === '' || substr($line, 0, 1) === '#') {
continue;
}
$equals_pos = strpos($line, '=');
if ($equals_pos !== false) {
$key = substr($line, 0, $equals_pos);
$value = substr($line, $equals_pos + 1);
if (substr($value, 0, 1) === '"' && substr($value, -1) === '"') {
$value = substr($value, 1, -1);
}
elseif (substr($value, 0, 1) === "'" && substr($value, -1) === "'") {
$value = substr($value, 1, -1);
}
$env_array[$key] = $value;
}
}
return $env_array;
}