Fungsi parse_ini_file
Fungsi parse_ini_file menerima laluan ke fail INI sebagai parameter pertama,
serta dua parameter pilihan: parameter kedua menentukan sama ada bahagian perlu diproses,
dan ketiga - mod pengimbasan fail.
Sintaks
parse_ini_file(
string $filename,
bool $process_sections = false,
int $scanner_mode = INI_SCANNER_NORMAL
);
Contoh
Mari buat fail config.ini dengan kandungan berikut:
; config.ini
database = mysql
host = localhost
user = root
password = 12345
Sekarang bacanya:
<?php
$res = parse_ini_file('config.ini');
print_r($res);
?>
Keputusan pelaksanaan kod:
[
'database' => 'mysql',
'host' => 'localhost',
'user' => 'root',
'password' => '12345'
]
Contoh
Sekarang tambahkan bahagian dalam fail INI kami:
; config.ini
[database]
type = mysql
host = localhost
[credentials]
user = root
password = 12345
Baca fail dengan pemprosesan bahagian:
<?php
$res = parse_ini_file('config.ini', true);
print_r($res);
?>
Keputusan pelaksanaan kod:
[
'database' => [
'type' => 'mysql',
'host' => 'localhost'
],
'credentials' => [
'user' => 'root',
'password' => '12345'
]
]
Contoh
Tunjukkan kerja dengan INI_SCANNER_TYPED:
; config.ini
debug = true
port = 3306
timeout = 3.5
Baca fail dengan penentuan jenis nilai:
<?php
$res = parse_ini_file('config.ini', false, INI_SCANNER_TYPED);
print_r($res);
?>
Keputusan pelaksanaan kod:
[
'debug' => true,
'port' => 3306,
'timeout' => 3.5
]
Lihat juga
-
fungsi
file_get_contents,
yang membaca fail ke dalam rentetan -
fungsi
file,
yang membaca fail ke dalam array