The full version of parse_ini_file () and Extension function parsing INI File

Source: Internet
Author: User

During PHP website development, INI parameter configuration files are often used to read. For example, you can directly modify parameters in the parameter configuration file by accessing complicated excuses, and then directly read and execute in the PHP script. PHP has a function that can directly read the ini configuration file, parse_ini_file (), and returns the result in an array. The following describes in detail how to use the PHP built-in function parse_ini_file to read the ini configuration file.

Parameter description: array parse_ini_file (string $ filename [, bool $ process_sections]) parse_ini_file () loads an INI file specified by filename and returns a union array. If the process_sections parameter is set to true, a multi-dimensional array is obtained, including the name and settings of each section in the configuration file. The default value of process_sections is false, and the array after each section is merged is returned. INI file annotation; symbol

The following describes the examples:

Config. ini (can be set to another type suffix) as follows:

; This is a sample configuration file; Comments start with ‘;‘, as in php.ini[first_section]one = 1five = 5animal = BIRD[second_section]path = "/usr/local/bin"URL = "http://www.example.com/~username"[third_section]phpversion[] = "5.0"phpversion[] = "5.1"phpversion[] = "5.2"phpversion[] = "5.3"

The PHP script test code is as follows:

<?php$ini_array = parse_ini_file("config.ini");print_r($ini_array);// Parse with sections$ini_array = parse_ini_file("config.ini", true);print_r($ini_array);

The running result is as follows:

Array([one] => 1[five] => 5[animal] => BIRD[path] => /usr/local/bin[URL] => http://www.9streets.cn)Array([test] => Array([one] => 1[five] => 5[animal] => BIRD)[beta] => Array([path] => /usr/local/bin[URL] => http://www.9streets.cn))


The parse_ini_file () function cannot parse the multidimensional array form in the INI file. Therefore, you need to manually add the following parse_ini_file_multi () method to parse the INI file larger than the 3D array. Note that an error exists in parse_ini_file_multi () in the PHP official manual, which has been fixed in the code.

However, I encountered another problem in the process of program development. I need to put the key-value pair of the multi-dimensional array parsed by the parse_ini_file () function into the interface URL for calling, in this way, the multi-dimensional array has a problem, and even if the effect can be achieved, it will face a very bloated traversal process.
Here, I use another method to convert the multidimensional array in the ini configuration file into a one-dimensional array, that is, the variable before "=, assign a value directly to a string of the type array style. The value after "=" is the final value of the multi-dimensional array.

The specific implementation and operation of this function, please check my personal website: http://www.phpthinking.com/archives/587

The full version of parse_ini_file () and Extension function parsing INI File

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.