PHP读写INI文件
读INI文件
public function readini($name)
{
if (file_exists(SEM_PATH.'init/'.$name)){
$data = parse_ini_file(SEM_PATH.'init/'.$name,true);
if ($data){
return $data;
}
}else {
return false;
}
}
写INI文件
function write_ini_file($assoc_arr, $path, $has_sections=FALSE) {
$content = "";
if ($has_sections) {
foreach ($assoc_arr as $key=>$elem) {
$content .= "[".$key."]n";
foreach ($elem as $key2=>$elem2) {
if(is_array($elem2))
{
for($i=0;$i<count($elem2);$i++)
{
$content .= $key2."[] = "".$elem2[$i].""n";
}
}
else if($elem2=="") $content .= $key2." = n";
else $content .= $key2." = "".$elem2.""n";
}
}
}
else {
foreach ($assoc_arr as $key=>$elem) {
if(is_array($elem))
{
for($i=0;$i<count($elem);$i++)
{
$content .= $key2."[] = "".$elem[$i].""n";
}
}
else if($elem=="") $content .= $key2." = n";
else $content .= $key2." = "".$elem.""n";
}
}
if (!$handle = fopen($path, 'w')) {
return false;
}
if (!fwrite($handle, $content)) {
return false;
}
fclose($handle);
return true;
}
用法
$sampleData = array(
'first' => array(
'first-1' => 1,
'first-2' => 2,
'first-3' => 3,
'first-4' => 4,
'first-5' => 5,
),
'second' => array(
'second-1' => 1,
'second-2' => 2,
'second-3' => 3,
'second-4' => 4,
'second-5' => 5,
));
write_ini_file($sampleData, './data.ini', true);
INI FILE Manage
http://www.phpclasses.org/browse/package/4232/download/targz.html
<?php
/**
* This file represents a class structure object hierarchy of
* and INI file manager. This set of object classes represent
* an INI file by:<br>
* <br>
* ini_Comment - A comment inside the INI file<br>
* ini_Value - A key/value pair represented by key=value<br>
* ini_Section - A section of an ini file represented by [section]<br>
* ini_File - A collection of ini_Section objects.<br>
* <br>
* Because reading a and parsing an INI file can take some time, you
* should <i>not</i> read an parse a file every time a web page is called! PHP
* does offer a solution! Sessions. To see how you can use this to your
* advantage, Take a look at this example:<br>
* <br>
* @example testpage.php
* <br><br>
* The above example shows you how to parse and output the information.<br>
* <br>
* This is a work in progress and has not been used in a production mode
* to manage INI files...yet. Therefore, I am looking for feedback to extend
* these objects to include methods that are needed to facilitate managing a
* remote INI file from your browser.<br>
* <br>
* The basic purpose of this object was to read an INI file in only once and
* perform a single action based on the settings in the INI file, not really
* remote INI file editing. But, let's have fun!<br>
* <br>
*
* @version 0.9.0b
* @author James Barwick <jbarwick@sentienthealth.com>
* @package ini_File
*/
define('INI_VALUE_TERMINATOR', 1);
define('INI_NEWLINE', 2);
define('INI_COMMENT_CHARACTER',3);
define('INI_MSG_NOFILE', 4);
define('INI_MSG_NOWRITE', 6);
define('INI_MSG_CANTWRITE', 7);
define('INI_ROOTSECTION', 5); $GLOBALS[INI_ROOTSECTION]="****root****";
$GLOBALS[INI_VALUE_TERMINATOR]="";
$GLOBALS[INI_NEWLINE]="\n";
$GLOBALS[INI_COMMENT_CHARACTER]=";"; $GLOBALS[INI_MSG_NOFILE]="File does not exist: ";
$GLOBALS[INI_MSG_NOWRITE]="Error writing file: ";
$GLOBALS[INI_MSG_CANTWRITE]="Cannot write to file: "; /**
* ini_Comment
*
* Represent comments within the ini_File as an object.
*
* @author James Barwick <jbarwick@sentienthealth.com>
* @package ini_File
* @copyright (c) Copyright 2007. James Barwick. All rights reserved.
*/
class ini_Comment {
/**
* The comment is an array of strings
* @var array()
*/
private $comment; /**
* Contructor
*
* Contruct the comment passing in at least one comment string
*
* @param string $comment
* @return ini_Comment
*/
function ini_Comment($comment) {
$this->setComment($comment);
} /**
* setComment
*
* Set the comment to the string passed in
*
* @param string $comment
* @access public
*/
function setComment($comment) {
$this->comment = array();
$this->addComment($comment);
} /**
* addComment
*
* Append a comment line to the array() list of comments.
*
* @param string $comment
* @access public
*/
function addComment($comment) {
$comment = ltrim($comment,"#,;");
array_push($this->comment,$comment);
} /**
* createComment
*
* Append a comment to the end of the specified
* comment object or create a new object.<br>
* <br>
* This is static so you can call it as:<br>
* <br>
* ini_Comment::createComment($c_obj,$comment);<br>
*
* @param ini_Comment $commentObject
* @param string $comment
* @return ini_Comment A new ini_Coment or the same one passed in
* @access public
*/
static function createComment($commentObject,$comment) {
if (empty($commentObject)) {
$commentObject = new ini_Comment($comment);
return $commentObject;
}
if ($commentObject instanceof ini_Comment) {
$commentObject->addComment($comment);
return $commentObject;
}
return null;
} /**
* toString
*
* Return a formatted string representing the comment lines.<br>
*<br>
* The comment is preceeded by $GLOBALS[INI_COMMENT_CHARACTER]<br>
*<br>
* Each comment line is ended with $GLOBALS[INI_NEWLINE]<br>
*
* @param integer $line_1_indent
* @param integer $line_2_indent
* @return string representing the coments
* @access public
*/
function toString($line_1_indent=0,$line_2_indent=0) {
$spaces = str_repeat(' ',$line_2_indent);
$s=str_repeat(' ',$line_1_indent);
$i=0;
foreach ($this->comment as $comment_line) {
if ($i==0) $i++; else $s.=$spaces;
if (empty($comment_line)){
$s.=$comment_line.$GLOBALS[INI_NEWLINE];
}else{
$s.=$GLOBALS[INI_COMMENT_CHARACTER].$comment_line.$GLOBALS[INI_NEWLINE];
}
}
return $s;
} } /**
* ini_Value
*
* @author James Barwick <jbarwick@sentienthealth.com>
* @package ini_File
* @copyright (c) Copyright 2007. James Barwick. All rights reserved.
*/
class ini_Value { /**
* Comment for this value. This value must start on the
* same line as the key but may contain multple lines.<br>
* <br>
* Example:<br>
* <br>
* mykey=myvalue #comment<br>
* #comment line 2<br>
* #comment line 3<br>
*
* @var ini_Comment
* @access private
*/
private $after_comment; /**
* Users sometimes put comments above a key value. This
* ini_Comment will be associated with with the key.
* <br>
* Example:<br>
* <br>
* #comment line 1<br>
* #comment line 2<br>
* key=value<br>
*
* @var ini_Comment
* @access private
*/
private $above_comment; /**
* The KEY for the VALUE
*
* @var string
* @access private
*/
private $name; /**
* The VALUE for the KEY
*
* @var string
* @access private
*/
private $content; /**
* Constructor for ini_Value
*
* @param string $name Name of value. This is called a Key
* @param string $content The value for the key
* @param string $comment The initial comment after the value
* @param mixed $parent Optional. ini_Section
* @access public
*/
function ini_Value($name, $content, $comment='')
{
$this->name = $name;
$this->content = $content;
$this->above_comment = null;
$this->after_comment = null;
if ($comment!='')
$this->addAfterComment($comment);
} // end constructor /**
* getName
*
* Get the key for this value.
*
* @return string
* @access public
*/
function getName() {
return $this->name;
} /**
* setBeforeComments
*
* Set the comments to the ini_Comments object.<br>
* <br>
* If the parameter passed is not a ini_Comment object,
* this function has no effect.
*
* @param ini_Comment $comments
* @return ini_Comment The comment above the key
* @access public
*/
function setBeforeComments($comments) {
if ($comments instanceof ini_Comment)
$this->above_comment = $comments; return $this->above_comment;
} /**
* setBeforComment
*
* Set the comments above this key to the string identified.
*
* @param string $comment
* @return ini_Comment The comment above the key
* @access public
*/
function setBeforeComment($comment) {
$this->above_comment = new ini_Comment($comment); return $this->above_comment;
} /**
* getComment
*
* Returns the comment AFTER this key value entry.
*
* @return ini_Comment object
* @access public
*/
function getComment() {
return $this->after_comment;
} /**
* addBeforeComment
*
* Add a comment above this value.
*
* @param string $comment
* @return ini_Comment Instance of ini_Comment object or null
* @access public
*/
function addBeforeComment($comment)
{
if (empty($this->above_comment))
$this->above_comment = new ini_Comment($comment);
else
$this->above_comment->setComment($comment); return $this->above_comment;
} /**
* addAfterComment
*
* Adds a comment to this item.
*
* @param string $comment The comment to add to this value
* @return ini_Comment reference to comment object
* @access public
*/
function addAfterComment($comment)
{
if (empty($this->after_comment))
$this->after_comment = new ini_Comment($comment);
else
$this->after_comment->addComment($comment); return $this->after_comment;
} /**
* setAfterComment
*
* Set the comments after this key to the comment provided.
*
* @param string $comment
* @return ini_Comment
* @access public
*/
function setAfterComment($comment) {
$this->after_comment = new ini_Comment($comment); return $this->after_comment;
} /**
* setContent
*
* Set the value for this key
*
* @param string $content
* @access public
*/
function setContent($content)
{
$this->content = $content;
} /**
* getContent
*
* Get the value for this key
*
* @return string vlaue for this key
* @access public
*/
function getContent()
{
return $this->content;
} /**
* toString
*
* Format the Value.<br>
* <br>
* This also includes the comments.
*
* @return string String representing the Value with its comments
* @access public
*/
function toString()
{
$s="";
if (!empty($this->above_comment))
$s.=$this->above_comment->toString(); $v=$this->name."=".$this->content.$GLOBALS[INI_VALUE_TERMINATOR];
$s.=$v;
if (!empty($this->after_comment)) {
$p = strlen($v)+1;
$s.=$this->after_comment->toString(1,$p);
}
else
$s.=$GLOBALS[INI_NEWLINE]; return $s;
}
} /**
* ini_Section
*
* @author James Barwick <jbarwick@sentienthealth.com
* @package ini_File
* @copyright (c) Copyright 2007. James Barwick. All rights reserved.
*/
class ini_Section { /**
* Container object name
* @var string
*/
private $name; /**
* An array of values indexed by the value's key
* @var string
*/
private $content; /**
* Comment for this section
* @var ini_Comment
*/
private $comment; /**
* Constructor
*
* @param mixed $parent ini_File object.
* @param string $name Name of the INI Section
* @param string $key Name of the Attribute
* @param string $content Value of the Attribute
* @access public
*/
function ini_Section($name, $comment = '')
{
$this->name = $name;
$this->content = array();
$this->comment = null;
if ($comment=='')
$this->addComment($comment);
} // end constructor /**
* addItem
*
* Adds an item to this section.
*
* @param string $name the key for the value
* @param string $value the value for the key
* @param string $comment Optional comment
* @return ini_Value Value modified or created
* @access public
*/
function addItem($name,$value,$comment='')
{
$key = strtolower($name); if (IsSet($this->content[$key])) {
$valueObject = $this->content[$key];
$valueObject->setContent($value);
$valueObject->setComment($comment);
}
else {
$valueObject = new ini_Value($name,$value,$comment);
$this->content[$key] = $valueObject;
} return $valueObject;
} /**
* setComment
*
* Set the comment object on this section to the comment provided
*
* @param ini_Comment $commnet This is the ini_Comment object
* @access public
*/
function setComment($comment) {
if ($comment instanceof ini_Comment)
$this->comment = $comment;
} /**
* addComment
*
* Adds a comment to this item.<Br>
* This is a helper method that calls createItem
*
* @param string $comment The comment for this section
* @return ini_Comment The comment object that was created.
* @access public
*/
function addComment($comment)
{
if (empty($this->comment))
$this->comment = new ini_Comment($comment);
else
$this->comment->addComment($comment); return $this->comment;
} /**
* addValue
*
* Add the value to the Section
*
* @param ini_Value $value Value to add to this section
* @access public
*/
function addValue($value) {
if ($value instanceof ini_Value) {
$key = strtolower($value->getName());
$this->content[$key]=$value;
}
} /**
* getItem
*
* Gets an item from it's key.<br>
* <br>
* Examples:<br>
* $value_object = $section->getItem('key');<br>
* $value = $section->getItem('key')->getValue();<br>
* $value = $inifile->getSection('section')->getItem('key')->getValue();<br>
*
* @param string $name The key of the item in this section to retrieve
* @return ini_Value The value referenced by this key
* @access public
*/
function getItem($name)
{
$key = strtolower($name); if (IsSet($this->content[$key]))
return $this->content[$key]; return null;
} /**
* getValues
*
* Return the values in this section in an associative array indexed by key.<br>
*<br>
* $values = array($key => new ini_Value($key,$value));<br>
*
* @return array() of ini_Value objects in an associative array indexed by key
* @access public
*/
function getValues() {
return $this->content;
} /**
* getName
*
* Get this Section's name.
*
* @return string item's name
* @access public
*/
function getName()
{
return $this->name;
} /**
* toString
*
* Return a string representation of the section of the ini file.
*
* @return string Formatted section of the INI file.
* @access public
*/
function toString()
{
$s="";
if (!empty($this->comment))
$s.=$this->comment->toString();
if ($this->name!=$GLOBALS[INI_ROOTSECTION])
$s.="[".$this->name."]".$GLOBALS[INI_NEWLINE];
foreach ($this->content as $value)
$s.=$value->toString();
return $s;
}
} /**
* ini_File
*
* This class allows for parsing and editing of ini files.
* This is meant to be streamlined and overhead elliminated.
*
* @author James Barwick
* @package ini_File
* @copyright (c) Copyright 2007. James Barwick. All rights reserved.
*/
class ini_File { /**
* File name of the file to read/write to.
*
* @var string
* @access private
*/
private $filename; /**
* ini_Section list. This is an associative array indexed by
* the sections name.
* <br>
* Example:<br>
* <br>
* $sections = array($key => new ini_Section($key,$value);
*
* @var array() of ini_Section objects
* @access private
*/
private $sections; /**
* Constructor
*
* Creates a holder for the ini_File contents.
*
* @param string $filename
* @access public
*/
function ini_File($filename)
{
$this->filename = $filename;
$this->sections = array();
} /**
* getSections
*
* Get the list of sections. There is always at least one section
* called $GLOBALS[INI_ROOTSECTION]. This section may be empty.
*
* @return array() An associative array of sections in the file.
* @access public
*/
function getSections() {
return $this->sections;
} /**
* getSection
*
* Get the section object ini_Section from its name.
*
* @param string $section Name or key of the section to get
* @return ini_Section
* @access public
*/
function getSection($section) {
$section = strtolower($section); if (IsSet($this->sections[$section])) {
return $this->sections[$section];
}
return null;
} /**
* getItem
*
* Retrieve the value from the section. Use empty string '' for the section name
* if the value isn't in a section.
*
* @param string $section name of the section to retrieve the value
* @param string $name name of the value to retreive
* @return ini_Value value identified by the name in the specified section
* @access public
*/
function getItem($section,$name) {
$section_Object = $this->getSection($section); if (empty($section_Object))
return null; return $section_Object->getItem($name);
} /**
* getItemValue
*
* Get the Value of the item in the specified section
*
* @param string $section Section name in the file
* @param string $name value name in the file
* @return string the value of the key
* @access public
*/
function getItemValue($section,$name) {
$value_object = $this->getItem($section,$name); if (empty($value_object))
return ''; return $value_object->getContent();
} /**
* read
*
* arses the data of the given configuration file
* @return string representing an error code or empty string if no error
* @access public
*/
function read() { if (!file_exists($this->filename))
return $GLOBALS[INI_MSG_NOFILE].$this->filename; $lines = file($this->filename); $this->parse($lines); return "";
} /**
* write
*
* Write ini contents to the file
*
* @return string representing an error code or empty string if no error
* @access public
*/
function write() {
$result = ""; if (!is_writable($this->filename))
return $GLOBALS[INI_MSG_CANTWRITE].$this->filename; $fl=fopen($this->filename,"w");
if (fputs($fl,$this->toString())===false) {
$result = $GLOBALS[INI_MSG_NOWRITE].$this->filename;
}
fclose($fl); return $result;
} /**
* createSection
*
* Adds the section or replaces the section specified.
*
* @param string $name
* @return ini_Section
* @access public
*/
function createSection($name) {
$key = trim(strtolower($name));
$s = new ini_Section($name);
$this->sections[$key] = $s; return $s;
} /**
* parse
*
* Parse an array() of INI File Lines
*
* @access public
* @param string array() $lines
*/
function parse($lines) { $this->sections = array(); $currentSection = $this->createSection($GLOBALS[INI_ROOTSECTION]);
$currentValue = null;
$currentComment = null; foreach ($lines as $line) { if (($line=='')||($line[0]=='\t')||($line[0]==' '))
$beginsWithSpaces=true;
else
$beginsWithSpaces=false; $line=trim($line,"\x00..\x20"); if ($line=='') {
$currentComment=ini_Comment::createComment($currentComment,$line);
continue;
} if ($line[0]==$GLOBALS[INI_COMMENT_CHARACTER]) {
if ($beginsWithSpaces) {
if (!empty($currentValue)) {
$currentValue->addAfterComment($line);
$currentComment=null;
continue;
}
}
$currentComment=ini_Comment::createComment($currentComment,$line);
continue;
} // Are we a section? if it begins with [ assume it is regardless
// of an ending ]. Result, fix bad INI section headers.
if ($line[0]=='[') {
$l=strlen(($line))-1;
if ($line[$l]==']') $l--;
$name = substr($line,1,$l); $currentSection=$this->createSection($name);
$currentSection->setComment($currentComment);
$currentComment=null;
continue;
} // Check for key
$i=strpos($line,'=');
if ($i==0) {
$currentComment=ini_Comment::createComment($currentComment,$line);
continue;
} $key=trim(substr($line,0,$i));
$line_length=strlen($line);
$inquote=false;
$value_start = $i+1;
$value_end = $value_start;
while ($value_end<$line_length)
{
$c=$line[$value_end];
if ($c=='"')
{
if (!$inquote)
$inquote=true;
else
$inquote=false;
}
if (!$inquote) {
if (($c==';')||($c=='#'))
break;
}
$value_end++;
} $value=substr($line,$value_start,$value_end-$value_start); if ($value_end<$line_length)
$comment=ltrim(substr($line,$value_end+1),"#,\x20");
else
$comment=""; $currentValue=$currentSection->addItem($key,$value,$comment); if (!empty($currentComment)) {
$currentValue->setBeforeComments($currentComment);
$currentComment=null;
}
}
} /**
* tpString
*
* Returns a formatted string of the file
*
* @return string
* @access public
*/
function toString()
{
$s="";
foreach ($this->sections as $section) {
$s.=$section->toString();
}
return trim($s);
}
}
?>
使用:
require_once('ini_File.php'); $filename = "./version.ini";
$inifile = new ini_File($filename);
$inifile->read();
$item = $inifile->getItem('notice','text');
$item->setContent($item->getContent()."測試測試中“");
$inifile->write();
使用ini_File.php更方便有效
PHP读写INI文件的更多相关文章
- c# 利用动态库DllImport("kernel32")读写ini文件(提供Dmo下载)
c# 利用动态库DllImport("kernel32")读写ini文件 自从读了设计模式,真的会改变一个程序员的习惯.我觉得嘛,经验也可以从一个人的习惯看得出来,看他的代码编写习 ...
- VB读写INI文件的四个函数以及相关API详细说明
WritePrivateProfileString函数说明 来源:http://blog.csdn.net/wjb9921/article/details/2005000 在我们写的程序当中,总有一 ...
- C# 读写INI 文件
INI 格式: [Section1] KeyWord1 = Value1 KeyWord2 = Value2 ... [Section2] KeyWord3 = Value3 KeyWord4 = V ...
- WIN32读写INI文件方法
在程序中经常要用到设置或者其他少量数据的存盘,以便程序在下一次执行的时候可以使用,比如说保存本次程序执行时窗口的位置.大小.一些用户设置的 数据等等,在 Dos 下编程的时候,我们一般自己产生一个 ...
- 读写ini文件
C# 使用文件流来读写ini文件 背景 之前采用ini文件作为程序的配置文件,觉得这种结构简单明了,配置起来也挺方便.然后操作方式是通过WindowsAPI,然后再网上找到一个基于WindowsAPI ...
- 在 WinCe 平台读写 ini 文件
在上篇文章开发 windows mobile 上的今日插件时,我发现 wince 平台上不支持例如 GetPrivateProfileString 等相关 API 函数.在网络上我并没有找到令我满意的 ...
- Windows中读写ini文件
.ini 文件是Initialization File的缩写,即初始化文件,是windows的系统配置文件所采用的存储格式,来配置应用软件以实现不同用户的要求.配置文件有很多种如ini配置文件,XML ...
- C#中读写INI文件
C#中读写INI文件 c#的类没有直接提供对ini文件的操作支持,可以自己包装win api的WritePrivateProfileString和GetPrivateProfileString函数实现 ...
- Qt读写ini文件
一 背景 1 ini文件介绍 .ini 文件是Initialization File的缩写,即初始化文件. 除了windows现在很多其他操作系统下面的应用软件也有.ini文件,用来配置应用软件以实现 ...
- 【python-ini】python读写ini文件
[python-ini]python读写ini文件 本文实例讲述了Python读写ini文件的方法.分享给大家供大家参考.具体如下: 比如有一个文件update.ini,里面有这些内容: 1 2 ...
随机推荐
- 远程执行shell
目前我的需求是在我的hadoop集群搭建起来前,能定时做一下简易指标的监控,目前我的方案就是在我的server机上,定时执行远程脚本出指标数据,然后通过python脚本发送邮件. 远程执行脚本如下: ...
- 详解VirtualBox虚拟机网络环境解析和搭建-NAT、桥接、Host-Only、Internal、端口映射
本文以VirtualBox为例 如果出现主机无法ping通虚拟机的情况,请首先确认虚拟机防火墙已关闭. 一.NAT模式 特点: 1.如果主机可以上网,虚拟机可以上网 2.虚拟机之间不能ping通 3. ...
- HDU 4725 The Shortest Path in Nya Graph (最短路)
The Shortest Path in Nya Graph Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- 给定一种 pattern(模式) 和一个字符串 str ,判断 str 是否遵循相同的模式。 这里的遵循指完全匹配,例如, pattern 里的每个字母和字符串 str 中的每个非空单词之间存在着双向连接的对应模式。
这个是LeetCode上的一道题目.本机上运行时正确的,但是LeetCode上显示是错误的,所以没有办法了只能记录在博客上了. 我的想法是先把pattern和str都转化成数组.例如"abb ...
- jquery.validate.js自定义表单验证
$(document).ready(function() { //在下列位置输入页面加载的逻辑代码 $("#inputForm").validate({ rules: { seq: ...
- 【教程】Tomcat 的catalina.out 日志按照自定义日期格式进行切割
本文简单介绍在使用cronolog对tomcat的日志进行自定义日期格式的切割,方便日志的整理和遇到问题日志的排查! 安装cronolog 安装cronolog的方法网上有很多,这里也简单的介绍一下. ...
- Android-Binder原理浅析
Android-Binder原理浅析 学习自 <Android开发艺术探索> 写在前头 在上一章,我们简单的了解了一下Binder并且通过 AIDL完成了一个IPC的DEMO.你可能会好奇 ...
- 捕获程序异常之tryCatch
一.try catch语法try…catch…finally…语法中除了try以外,catch和finally都是可选的(两者必须要有一个),也就是说try…catch…finally…语法有以下三种 ...
- [POI2013]Usuwanka
[POI2013]Usuwanka 题目大意: 一排\(n\)个球,有黑白两种颜色.每取走一个球会在原位置放一个水晶球.求构造一种取球方案,满足: 每次取走\(k\)个白球和\(1\)个黑球: 一次取 ...
- [HDU2138]How many prime numbers
来源: HDU 2007-11 Programming Contest_WarmUp 题目大意:素数判定. 思路: 事实上暴力判定也可以过,但我还是用了Miller-Rabin算法. 核心思想:利用费 ...