这是最初的字符串: -如何从字符串中获取关联数组?
NAME=Marco\nLOCATION=localhost\nSECRET=fjsdgfsjfdskffuv=\n
这是我的解决方案虽然“=”的字符串的结束并不阵列
$env = file_get_contents(base_path() . '/.env');
// Split string on every " " and write into array
$env = preg_split('/\s+/', $env);
//create new array to push data in the foreach
$newArray = array();
foreach($env as $val){
// Split string on every "=" and write into array
$result = preg_split ('/=/', $val);
if($result[0] && $result[1])
{
$newArray[$result[0]] = $result[1];
}
}
print_r($newArray);
这似乎是结果我得到:
Array ([Name] => Marco [LOCATION] => localhost [SECRET] => fjsdgfsjfdskffuv)
但我需要:
Array ([Name] => Marco [LOCATION] => localhost [SECRET] => fjsdgfsjfdskffuv=)
http://php.net/manual/en/function.parse-url.php尝试使用此功能 – Zeljka
这是我得到的parse_url()函数:Array([path] => NAME = Marco_LOCATION = localhost_SECRET = fjsdgfsjfdskffuv = _) – Marco
@仍然死我没有降级你的答案。你为什么要这样对我的问题呢? – Marco