将文件解析为数组时的额外空白值
Extra blank value when parsing file to array
编辑: 检查 !empty 有效 - 但我仍然想知道为什么 while 似乎在文件结束后继续。谢谢!
我正在尝试解析一个如下所示的文件:
export NTPSERVER_1 NTPSERVER_2 NTPSERVER_3 PSLOGHOST LOGHOST RSSHHOST RHPORT
NTPSERVER_1=8.8.8.8
NTPSERVER_2=
NTPSERVER_3=
LOGHOST="8.8.8.8"
PSLOGHOST=""
RSSHHOST="8.8.8.8"
RHPORT=88888
它工作得很好,除了数组中有一个额外的最后一个值没有键和一个空值。我尝试添加检查 $line 是否为 null 但无济于事。我已经仔细检查过该文件在 RHPORT 行之后没有任何空行。
我收到一条 "Notice: Undefined offset: 1" 消息,数组中的最后一件事是 [""]=> NULL
我不明白为什么 while 似乎没有在文件末尾停止。
$file = fopen("files/network.conf","r");
$i = 0;
while(! feof($file)) {
$line = fgets($file);
if ($i > 0 && !is_null($line)) { // skipping first line of file
$array = explode('=',$line);
$fileValues[$array[0]] = $array[1];
}
$i++;
}
fclose($file);
我会推荐你使用file()函数
$array = file('file path', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$final_array = [];//empty array declaration
foreach($array as $arr){ // iterate over array get from file() function
$exploded_array = explode('=',$arr);
$final_array[$exploded_array[0]] = $exploded_array[1];
}
print_r($final_array);
如你所见,只需要一行代码,你就会得到想要的数组
您可以使用 file_get_contents 和正则表达式来获取值。
// Uncomment line below
//$str =file_get_contents("files/network.conf");
$str = 'export NTPSERVER_1 NTPSERVER_2 NTPSERVER_3 PSLOGHOST LOGHOST RSSHHOST RHPORT
NTPSERVER_1=8.8.8.8
NTPSERVER_2=
NTPSERVER_3=
LOGHOST="8.8.8.8"
PSLOGHOST=""
RSSHHOST="8.8.8.8"
RHPORT=88888';
// Find lines with = and save them to $matches[1] and [2]
Preg_match_all("/(\w+)\=(.*)/", $str, $matches);
// Create array as you expect
Foreach($matches[1] as $key => $val){
$res[$val] = str_replace('"','',$matches[2][$key]); // remove " if it exists.
}
Var_dump($res);
编辑: 检查 !empty 有效 - 但我仍然想知道为什么 while 似乎在文件结束后继续。谢谢!
我正在尝试解析一个如下所示的文件:
export NTPSERVER_1 NTPSERVER_2 NTPSERVER_3 PSLOGHOST LOGHOST RSSHHOST RHPORT
NTPSERVER_1=8.8.8.8
NTPSERVER_2=
NTPSERVER_3=
LOGHOST="8.8.8.8"
PSLOGHOST=""
RSSHHOST="8.8.8.8"
RHPORT=88888
它工作得很好,除了数组中有一个额外的最后一个值没有键和一个空值。我尝试添加检查 $line 是否为 null 但无济于事。我已经仔细检查过该文件在 RHPORT 行之后没有任何空行。 我收到一条 "Notice: Undefined offset: 1" 消息,数组中的最后一件事是 [""]=> NULL 我不明白为什么 while 似乎没有在文件末尾停止。
$file = fopen("files/network.conf","r");
$i = 0;
while(! feof($file)) {
$line = fgets($file);
if ($i > 0 && !is_null($line)) { // skipping first line of file
$array = explode('=',$line);
$fileValues[$array[0]] = $array[1];
}
$i++;
}
fclose($file);
我会推荐你使用file()函数
$array = file('file path', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$final_array = [];//empty array declaration
foreach($array as $arr){ // iterate over array get from file() function
$exploded_array = explode('=',$arr);
$final_array[$exploded_array[0]] = $exploded_array[1];
}
print_r($final_array);
如你所见,只需要一行代码,你就会得到想要的数组
您可以使用 file_get_contents 和正则表达式来获取值。
// Uncomment line below
//$str =file_get_contents("files/network.conf");
$str = 'export NTPSERVER_1 NTPSERVER_2 NTPSERVER_3 PSLOGHOST LOGHOST RSSHHOST RHPORT
NTPSERVER_1=8.8.8.8
NTPSERVER_2=
NTPSERVER_3=
LOGHOST="8.8.8.8"
PSLOGHOST=""
RSSHHOST="8.8.8.8"
RHPORT=88888';
// Find lines with = and save them to $matches[1] and [2]
Preg_match_all("/(\w+)\=(.*)/", $str, $matches);
// Create array as you expect
Foreach($matches[1] as $key => $val){
$res[$val] = str_replace('"','',$matches[2][$key]); // remove " if it exists.
}
Var_dump($res);