顺晟科技
2021-06-16 10:31:51
290
代码:
?服务器端编程语言(专业超文本预处理器的缩写)
/**
* PHP操作初始化设置文件的后缀名文件类
* @作者维吉森-心语难诉
v1.0版
* @ link https://xinyu19.com
* 注:ini文件由节、键、值组成,为了方便
* 类中的[节]我们叫做[分类],[键=值]称为[子项]
*/
配置文件类
{
public $ iniFilePath
public $ iniFileHandle
函数_ _构造($iniFilePath)
{
$ this-iniFilePath=$ iniFilePath;
# 读入。初始化设置文件的后缀名文件到句柄中
if(file _ exists($ this-iniFilePath)){
$ this-inifile句柄=parse _ ini _ file($ this-ini file path,true);
if(empty($ this-ini file handle))die($ this-ini file path .文件为null ');
}else{
die($this-iniFilePath .文件无法打开');
}
}
//增加分类
公共函数addCategory($category_name,array $item=[]){
if(!isset($ this-IniFileHandle[$ category _ name]){
$ this-inifile句柄[$ category _ name]=[];
}else{
if(!空($item)) {
foreach ($item as $key=$value) {
$ this-IniFileHandle[$ category _ name][$ key]=$ value;
}
}
}
$ this-save();
}
//增加子项[可在添加分类的同时添加子项]
public function addItem($ category _ name,array $item){
foreach ($item as $key=$value) {
$ this-IniFileHandle[$ category _ name][$ key]=$ value;
}
$ this-save();
}
//获取所有
公共函数getAll(){
返回$ this-InifileHandle;
}
//获取单个分类
公共函数getCategory($category_name){
返回$ this-IniFileHandle[$ category _ name];
}
//获取子项值
公共函数getItem($category_name,$item_name){
# 如果是获取多个子项,则循环读取放入新变量
if (is_array($item_name)) {
$ arr=array();
foreach ($item_name as $value) {
$ arr[$ value]=$ this-IniFileHandle[$ category _ name][$ value];
}
返回$ arr
}else{
返回$ this-IniFileHandle[$ category _ name][$ item _ name];
}
}
//更改初始化设置文件的后缀名
public function updItem($ category _ name,array $item){
foreach ($item as $key=$value) {
$ this-IniFileHandle[$ category _ name][$ key]=$ value;
}
$ this-save();
}
//删除分类
公共函数delCategory($category_name){
取消设置($ this-IniFileHandle[$ category _ name]);
$ this-save();
}
//删除子项
public function Delitem($ category _ name,$item_name) {
取消设置($ this-inifilehead[$ category _ name][$ item _ name]);
$ this-save();
}
//保存。初始化设置文件的后缀名文件
公共函数save(){
$ string=
# 循环句柄,拼接成初始化设置文件的后缀名格式的字符串
foreach($ this-Inifilehandle as $ key=$ value){
$string .='['.$key .']'.\ r \ n ';
foreach ($value as $k=$v) {
$string .=' $ k=$ v \ r \ n
}
}
$ IniFileHandle=fopen($ this-IniFilePath,' w ');
$ is write=fwrite($ IniFileHandle,$ string);
if($ is write){
fc lose($ IniFileHandle);
返回真实的
}else{
fc lose($ IniFileHandle);
返回错误的
}
}
}
?
使用方式:
# 实例化初始化设置文件的后缀名文件操作类,并载入。初始化设置文件的后缀名文件
$iniFile=新的iniFile(' ./config。ini ');
# 添加一个分类
$ IniFile-AddCategory(' config ');
# 添加一个分类并直接添加子项
$iniFile-addCategory('config ',['test1'=123,' test 2 '=456]);
# 添加一个子项(如果子项存在,则覆盖;)
$iniFile-addItem('config ',[' test 1 '=123]);
# 删除一个分类
$ IniFile-DelCategory(' config ');
# 删除一个子项
$iniFile-delItem('config ',' test1 ');
# 修改一个分类下子项的值(也可以修改多个)
$iniFile-updItem('config ',[' test 1 '=789]);
# 获取一个分类下所有数据
print _ r($ Inifile-GetCategory(' config ');
# 获取一个分类下某个子项的值
print _ r($ Inifile-GetItem(' config ',' test1 ');
# 获取一个分类下多个子项的值
print _ r($ Inifile-GetItem(' config ',['test1 ',' test2 ']);
# 获取初始化设置文件的后缀名文件中所有分类及子项
print _ r($ Inifile-GetAll());
# 或者
print _ r($ Inifile-Inifilehandle);
08
1972-02
02
2022-09
12
2021-11
28
2021-08
28
2021-08
28
2021-08