Code for operations such as reading and modifying the config. php file. For more information, see
The code is as follows:
$ Name = "admin"; // kkkk
$ Bb = '000000 ';
$ Db = 4561321;
$ Kkk = "admin ";
?>
Function definition:
Configuration file data value acquisition: function getconfig ($ file, $ ini, $ type = "string ")
Configuration file data item update: function updateconfig ($ file, $ ini, $ value, $ type = "string ")
Call method:
The code is as follows:
Getconfig ("./2.php"," bb ");//
Updateconfig ("./2.php"," kkk "," admin ");
The code is as follows:
// Obtain the configuration file data value.
// If no third parameter exists by default, extract the content in ''or" "according to the string
// If there is a third parameter, int is used.
Function getconfig ($ file, $ ini, $ type = "string ")
{
If ($ type = "int ")
{
$ Str = file_get_contents ($ file );
$ Config = preg_match ("/". $ ini. "= (. *);/", $ str, $ res );
Return $ res [1];
}
Else
{
$ Str = file_get_contents ($ file );
$ Config = preg_match ("/". $ ini. "= \" (. *) \ ";/", $ str, $ res );
If ($ res [1] = null)
{
$ Config = preg_match ("/". $ ini. "= '(. *)';/", $ str, $ res );
}
Return $ res [1];
}
}
// Update configuration file data items
// If no fourth parameter exists by default, extract the content in ''or" "according to the string
// If there is a fourth parameter, int is used.
Function updateconfig ($ file, $ ini, $ value, $ type = "string ")
{
$ Str = file_get_contents ($ file );
$ Str2 = "";
If ($ type = "int ")
{
$ Str2 = preg_replace ("/". $ ini. "= (. *);/", $ ini. "=". $ value. ";", $ str );
}
Else
{
$ Str2 = preg_replace ("/". $ ini. "= (. *);/", $ ini. "= \"". $ value. "\"; ", $ str );
}
File_put_contents ($ file, $ str2 );
}
// Echo getconfig ("./2.php"," bb "," string ");
Getconfig ("./2.php"," bb ");//
Updateconfig ("./2.php"," kkk "," admin ");
// Echo"
". Getconfig ("./2.php", "name", "string ");
?>
The code is as follows:
// Complete the ultimate version
/**
* Configuration file operations (query and modification)
* If no third parameter is provided by default, the content in ''or" "is extracted based on the string.
* If there is a third parameter, int is used.
* Call demo
$ Name = "admin"; // kkkk
$ Bb = '000000 ';
$ Bb = getconfig ("./2.php"," bb "," string ");
Updateconfig ("./2.php"," name "," admin ");
*/
Function get_config ($ file, $ ini, $ type = "string "){
If (! File_exists ($ file) return false;
$ Str = file_get_contents ($ file );
If ($ type = "int "){
$ Config = preg_match ("/". preg_quote ($ ini). "= (. *);/", $ str, $ res );
Return $ res [1];
}
Else {
$ Config = preg_match ("/". preg_quote ($ ini). "= \" (. *) \ ";/", $ str, $ res );
If ($ res [1] = null ){
$ Config = preg_match ("/". preg_quote ($ ini). "= '(. *)';/", $ str, $ res );
}
Return $ res [1];
}
}
Function update_config ($ file, $ ini, $ value, $ type = "string "){
If (! File_exists ($ file) return false;
$ Str = file_get_contents ($ file );
$ Str2 = "";
If ($ type = "int "){
$ Str2 = preg_replace ("/". preg_quote ($ ini ). "= (. *);/", $ ini. "= ". $ value. ";", $ str );
}
Else {
$ Str2 = preg_replace ("/". preg_quote ($ ini ). "= (. *);/", $ ini. "= \"". $ value. "\"; ", $ str );
}
File_put_contents ($ file, $ str2 );
}