PHP:读取ini xml等配置文件
我们在开发程序的时候会经常用到数据存储这一块。一般呢,如果数据不多,我们一般都会选择把数据把数据保存在本地的配置上面。而配置文件最常见到的格式主要有ini xml等几种格式。大家为什么把数据保存在文本类型的文件里面,我也不知道。也许是读取速度上面
我们在开发程序的时候会经常用到数据存储这一块。一般呢,如果数据不多,我们一般都会选择把数据把数据保存在本地的配置上面。而配置文件最常见到的格式主要有ini xml等几种格式。大家为什么把数据保存在文本类型的文件里面,我也不知道。也许是读取速度上面的差异吧。好了,下面我们来看看,PHP怎么从本地的配置文件里面读取数据,并获得自己所需的部分呢。
<?php class Settings { var $_settings = array (); function get($var) { $var = explode ( ‘.’, $var ); $result = $this->_settings; foreach ( $var as $key ) { if (! isset ( $result [$key] )) { return false; } $result = $result [$key]; } return $result; } function load() { trigger_error ( ’Not yet implemented’, E_USER_ERROR ); } } class Settings_PHP extends Settings { function load($file) { if (file_exists ( $file ) == false) { return false; } // Include file include ($file); unset ( $file ); // Get declared variables $vars = get_defined_vars (); // Add to settings array foreach ( $vars as $key => $val ) { if ($key == ’this’) continue; $this->_settings [$key] = $val; } } } class Settings_INI extends Settings { function load($file) { if (file_exists ( $file ) == false) { return false; } $this->_settings = parse_ini_file ( $file, true ); } } class Settings_YAML extends Settings { function load($file) { if (file_exists ( $file ) == false) { return false; } include (‘spyc.php’); $this->_settings = Spyc::YAMLLoad ( $file ); } } class Settings_XML extends Settings { function load($file) { if (file_exists ( $file ) == false) { return false; } include (‘xmllib.php’); $xml = file_get_contents ( $file ); $data = XML_unserialize ( $xml ); $this->_settings = $data ['settings']; } } ?> php /** * 针对PHP的配置,如有配置文件 *config.php <?php $db = array(); // Enter your database name here: $db['name'] = ’test’; // Enter the hostname of your MySQL server: $db['host'] = ’localhost’; ?> //具体调用: include (‘settings.php’); //原始环境假设每个类为单独的一个类名.php文件 // Load settings (PHP) $settings = new Settings_PHP; $settings->load(‘config.php’); echo ’PHP: ’ . $settings->get(‘db.host’) . ”; * */ 读取INI文件,主要用到parser_ini_file函数,该函数返回一个数组,如第二个参数为true时则返回多维数组 /** * ini例子:config.ini * [db] name = test host = localhost //调用例子: $settings = new Settings_INI; $settings->load(‘config.ini’); echo ’INI: ’ . $settings->get(‘db.host’) . ”; */ 读取XML文件,需要用到XML_PARSER,xmllib.php在http://hudeyong926.iteye.com/admin/blogs/836048 /** * XML例子:config.xml <?xml version=”1.0″ encoding=”UTF-8″?> <settings> <db> <name>test</name> <host>localhost</host> </db> </settings> // Load settings (XML) $settings = New Settings_XML; $settings->load(‘config.xml’); echo ’XML: ’ . $settings->get(‘db.host’) . ”; * */ 读取YAML格式文件,使用YAML必须使用到SPYC这个库,相关链接在http://spyc.sourceforge.net/ /** YAML配置例子:config.yaml db: name: test host: localhost // Load settings (YAML) $settings = New Settings_YAML; $settings->load(‘config.yaml’); echo ’YAML: ’ . $settings->get(‘db.host’) . ”; */ 1。ini有点过时?? 2。xml比较好, 3。yaml很好,但是毕竟没有标准化。 4。txt要自己组织格式,开放性不好。 5。类序列化。比较好,但是不熟悉的人使用比较麻烦! 6。php定义常量(你不用修改数据吗?) 所以:xml最好。
当然了,如果数据庞大,我们就需要借助数据库来保存数据了。为了执行速度,更为了安全……
转载请注明:IT路人 » PHP:读取ini xml等配置文件

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
