ฟังก์ชัน parse_ini_file

ฟังก์ชัน parse_ini_file รับเส้นทางไปยังไฟล์ INI เป็นพารามิเตอร์แรก, และมีพารามิเตอร์ที่ไม่บังคับอีกสองตัว: พารามิเตอร์ตัวที่สองกำหนดว่าจำเป็นต้องประมวลผลส่วนต่างๆ หรือไม่, และตัวที่สาม - โหมดการสแกนไฟล์

ไวยากรณ์

phpparse_ini_file(
  string $filename,
  bool $process_sections = false,
  int $scanner_mode = INI_SCANNER_NORMAL
);

ตัวอย่าง

สร้างไฟล์ config.ini ด้วยเนื้อหาดังต่อไปนี้:

php; config.ini
database = mysql
host = localhost
user = root
password = 12345

ทีนี้มาอ่านมัน:

php<?php
  $res = parse_ini_file('config.ini');
  print_r($res);
?>

ผลลัพธ์จากการรันโค้ด:

php[
  'database' => 'mysql',
  'host' => 'localhost',
  'user' => 'root',
  'password' => '12345'
]

ตัวอย่าง

ตอนนี้เพิ่มส่วนต่างๆ ลงในไฟล์ INI ของเรา:

php; config.ini
[database]
type = mysql
host = localhost

[credentials]
user = root
password = 12345

อ่านไฟล์ด้วยการประมวลผลส่วนต่างๆ:

php<?php
  $res = parse_ini_file('config.ini', true);
  print_r($res);
?>

ผลลัพธ์จากการรันโค้ด:

php[
  'database' => [
    'type' => 'mysql',
    'host' => 'localhost'
  ],
  'credentials' => [
    'user' => 'root',
    'password' => '12345'
  ]
]

ตัวอย่าง

สาธิตการทำงานกับ INI_SCANNER_TYPED:

php; config.ini
debug = true
port = 3306
timeout = 3.5

อ่านไฟล์ด้วยการกำหนดประเภทค่าต่างๆ:

php<?php
  $res = parse_ini_file('config.ini', false, INI_SCANNER_TYPED);
  print_r($res);
?>

ผลลัพธ์จากการรันโค้ด:

php[
  'debug' => true,
  'port' => 3306,
  'timeout' => 3.5
]

ดูเพิ่มเติม

  • ฟังก์ชัน file_get_contents,
    ซึ่งอ่านไฟล์เป็นสตริง
  • ฟังก์ชัน file,
    ซึ่งอ่านไฟล์เป็นอาเรย์