Configuration File Operations in php, such as reading and modifying the config. php file

Source: Internet
Author: User
Code for operations such as reading and modifying the config. php file. For more information, see

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 );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.