这个php文件有删除节点、添加节点、修改节点、修改节点名字的功能,读取数组修改(操作数组),不适合大文件。不是很完善,只做参考,需要自己完善
类
<?php
Class operateIni{
private $iniInfo;
private $filename;
private $iniText;
function __construct($filename){
$this->filename = $filename;
$this->iniInfo = parse_ini_file($filename, true);
}
function delIniNode($Node){
unset($this->iniInfo[$Node]);
}
function addIniNode($Node, $addData){
//addData is Array()
$this->iniInfo[$Node] = $addData;
}
function updateIniData($Node, $updateName, $updateData){
$this->iniInfo[$Node][$updateName] = $updateData;
}
function delIniData($Node, $delName){
unset($this->iniInfo[$Node][$delName]);
}
function writeIni(){
foreach ($this->iniInfo as $iniNode => $iniNodeValue) {
$this->iniText .= "[{$iniNode}]\r\n";
foreach ($iniNodeValue as $name => $value) {
$this->iniText .= $name . "=" . $value . "\r\n";
}
}
file_put_contents($this->filename, $this->iniText);
}
}
?>
函数
<?php
//删除节点delini(ini文件名,节点名字)
function delini($ininame,$delnode){
$config=parse_ini_file($ininame,true);
$configtext="";
foreach ($config as $node => $nodevalue) {
if($node==$delnode){
continue;
}
$configtext.="[$node]\n";
foreach ($nodevalue as $name => $value) {
$configtext.=$name."=".$value."\n";
}
}
file_put_contents("config/index.ini", $configtext);
}
//添加节点数据addini(ini文件名,数据)
function addini($ininame,$addnode){
//添加数据类型[nodename]a=a b=b
file_put_contents($ininame,$addnode,FILE_APPEND);
}
//修改节点内数据updateini(ini文件名,节点名字,需要修改字段,内容)
function updateini($ininame,$updatenode,$updatename,$updatetext){
$config=parse_ini_file($ininame,true);
$configtext="";
foreach ($config as $node => $nodevalue) {
$configtext.="[$node]\n";
if($node==$updatenode){
foreach ($nodevalue as $name => $value) {
if($name==$updatename){
$configtext.=$name."=".$updatetext."\n";
}else{
$configtext.=$name."=".$value."\n";
}
}
}else{
foreach ($nodevalue as $name => $value) {
$configtext.=$name."=".$value."\n";
}
}
}
file_put_contents("config/index.ini", $configtext);
}
//修改节点名字upnodeini(ini文件名,节点名字,内容)
function upnodeini($ininame,$updatanode,$updatetext){
$config=parse_ini_file($ininame,true);
$configtext="";
foreach ($config as $node => $nodevalue) {
if($node==$updatanode){
$configtext.="[$updatetext]\n";
}else{
$configtext.="[$node]\n";
}
foreach ($nodevalue as $name => $value) {
$configtext.=$name."=".$value."\n";
}
}
file_put_contents("config/index.ini", $configtext);
}
?>
评论区