PHP in the configuration file operations such as config.php file read modification and other operations _php skills

Source: Internet
Author: User
Tags function definition
Copy Code code as follows:

<?php
$name = "admin";//kkkk
$BB = ' 234 ';
$db = 4561321;
$KKK = "admin";
?>

function definition:
Profile data value fetch: function GetConfig ($file, $ini, $type = "string")
Profile Data Item Update: function Updateconfig ($file, $ini, $value, $type = "string")
Call Mode:
Copy Code code as follows:

GetConfig ("./2.php", "BB");
Updateconfig ("./2.php", "KKK", "admin");

Copy Code code as follows:

<?php

The profile data value is obtained.
The contents of the extract ' or ' in ' are read by string when there is no third parameter by default
If the third argument is int, it is treated as a numeric int.
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];
}
}

Profile Data Item Update
When the default does not have the fourth parameter, the content in the extract ' or ' is read by string
If the fourth argument is int, it is treated as a numeric int.
function Updateconfig ($file, $ini, $value, $type = "string")
{
$str = file_get_contents ($file);
$str 2 = "";
if ($type = = "int")
{
$str 2 = preg_replace ("/". $ini. "= (. *);/", $ini. " =" . $value. ";", $str);
}
Else
{
$str 2 = preg_replace ("/". $ini. "= (. *);/", $ini. " =\"" . $value. "\"; ", $str);
}
File_put_contents ($file, $str 2);
}


Echo GetConfig ("./2.php", "BB", "string");
GetConfig ("./2.php", "BB");
Updateconfig ("./2.php", "KKK", "admin");
echo "<br/>". GetConfig ("./2.php", "Name", "string");

?>

Copy Code code as follows:

Improve the improved version


/**
* Profile operation (queried and modified)
* The content in the extract ' or ' from the string is read by default when there is no third parameter
* If the third parameter is int, it is treated as a numeric int.
* Invoke Demo
$name = "admin";//kkkk
$BB = ' 234 ';

$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);
$str 2 = "";
if ($type = = "int") {
$str 2 = preg_replace ("/". Preg_quote ($ini). = (. *);/", $ini." = ". $value."; ", $str);
}
else{
$str 2 = preg_replace ("/". Preg_quote ($ini). = (. *);/", $ini." =\ "". $value. " \ ";", $str);
}
File_put_contents ($file, $str 2);
}

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.