Inhaltsverzeichnis
2. [代码][PHP]代码    
Heim php教程 PHP源码 配置文件操作.如config.php文件的读取修改等操作

配置文件操作.如config.php文件的读取修改等操作

May 25, 2016 pm 05:13 PM

对形如config.php文件的读取,修改等操作 

<?php

$name="admin";//kkkk

$bb='234';

$db=4561321;

$kkk="admin";

?>


函数定义: 
    配置文件数据值获取:function getconfig($file, $ini, $type="string") 
    配置文件数据项更新:function updateconfig($file, $ini, $value,$type="string") 

调用方式: 

    getconfig("./2.php", "bb");//

    updateconfig("./2.php", "kkk", "admin");

                   

1. [代码][PHP]代码   

&lt;?php

//配置文件数据值获取。
//默认没有第三个参数时,按照字符串读取提取&#39;&#39;中或&quot;&quot;中的内容
//如果有第三个参数时为int时按照数字int处理。
function getconfig($file, $ini, $type=&quot;string&quot;)
{
	if ($type==&quot;int&quot;)
	{
		$str = file_get_contents($file);
		$config = preg_match(&quot;/&quot; . $ini . &quot;=(.*);/&quot;, $str, $res);
		Return $res[1];
	}
	else
	{
		$str = file_get_contents($file);
		$config = preg_match(&quot;/&quot; . $ini . &quot;=\&quot;(.*)\&quot;;/&quot;, $str, $res);
		if($res[1]==null)
		{	
			$config = preg_match(&quot;/&quot; . $ini . &quot;=&#39;(.*)&#39;;/&quot;, $str, $res);
		}
		Return $res[1];
	}
} 

//配置文件数据项更新
//默认没有第四个参数时,按照字符串读取提取&#39;&#39;中或&quot;&quot;中的内容
//如果有第四个参数时为int时按照数字int处理。
function updateconfig($file, $ini, $value,$type=&quot;string&quot;)
{
	$str = file_get_contents($file);
	$str2=&quot;&quot;;
	if($type==&quot;int&quot;) 
	{	
		$str2 = preg_replace(&quot;/&quot; . $ini . &quot;=(.*);/&quot;, $ini . &quot;=&quot; . $value . &quot;;&quot;, $str);
	}
	else 
	{
		$str2 = preg_replace(&quot;/&quot; . $ini . &quot;=(.*);/&quot;, $ini . &quot;=\&quot;&quot; . $value . &quot;\&quot;;&quot;, $str);
	}
	file_put_contents($file, $str2);
} 


//echo getconfig(&quot;./2.php&quot;, &quot;bb&quot;, &quot;string&quot;);
getconfig(&quot;./2.php&quot;, &quot;bb&quot;);//
updateconfig(&quot;./2.php&quot;, &quot;kkk&quot;, &quot;admin&quot;);
//echo &quot;&lt;br/&gt;&quot;.getconfig(&quot;./2.php&quot;, &quot;name&quot;,&quot;string&quot;);

?&gt;
Nach dem Login kopieren


2. [代码][PHP]代码

//完善改进版


/**
 * 配置文件操作(查询了与修改)
 * 默认没有第三个参数时,按照字符串读取提取&#39;&#39;中或&quot;&quot;中的内容
 * 如果有第三个参数时为int时按照数字int处理。
 *调用demo
	$name=&quot;admin&quot;;//kkkk
	$bb=&#39;234&#39;;
	
	$bb=getconfig(&quot;./2.php&quot;, &quot;bb&quot;, &quot;string&quot;);
	updateconfig(&quot;./2.php&quot;, &quot;name&quot;, &quot;admin&quot;);
*/
function get_config($file, $ini, $type=&quot;string&quot;){
	if(!file_exists($file)) return false;
	$str = file_get_contents($file);
	if ($type==&quot;int&quot;){
		$config = preg_match(&quot;/&quot;.preg_quote($ini).&quot;=(.*);/&quot;, $str, $res);
		return $res[1];
	}
	else{
		$config = preg_match(&quot;/&quot;.preg_quote($ini).&quot;=\&quot;(.*)\&quot;;/&quot;, $str, $res);
		if($res[1]==null){	
			$config = preg_match(&quot;/&quot;.preg_quote($ini).&quot;=&#39;(.*)&#39;;/&quot;, $str, $res);
		}
		return $res[1];
	}
}

function update_config($file, $ini, $value,$type=&quot;string&quot;){
	if(!file_exists($file)) return false;
	$str = file_get_contents($file);
	$str2=&quot;&quot;;
	if($type==&quot;int&quot;){	
		$str2 = preg_replace(&quot;/&quot;.preg_quote($ini).&quot;=(.*);/&quot;, $ini.&quot;=&quot;.$value.&quot;;&quot;,$str);
	}
	else{
		$str2 = preg_replace(&quot;/&quot;.preg_quote($ini).&quot;=(.*);/&quot;,$ini.&quot;=\&quot;&quot;.$value.&quot;\&quot;;&quot;,$str);
	}
	file_put_contents($file, $str2);
}
Nach dem Login kopieren

                   

                   

Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn

Heiße Artikel -Tags

Notepad++7.3.1

Notepad++7.3.1

Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version

SublimeText3 chinesische Version

Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1

Senden Sie Studio 13.0.1

Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6

Dreamweaver CS6

Visuelle Webentwicklungstools

SublimeText3 Mac-Version

SublimeText3 Mac-Version

Codebearbeitungssoftware auf Gottesniveau (SublimeText3)