這是最初的字符串: -如何從字符串中獲取關聯數組?
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