When I encountered a problem, I think there must be a simple way of writing, but I couldn't find it at the moment. Config. phparray (& nbsp; 'title = '& gt; "123", & nbsp; 'title2' = & gt; "123", & nbsp; & nbsp; 'title3 encountered a problem. I think there must be a simple method, but I cannot find it at the moment.
Config. php
Array (
& Apos; title = & apos; 123 & apos ",
'Title2' => "123 ",
'Title3' => "123 ",
......
Unlimited
)
In this case, I only want to change the value of title2, for example, to 456.
Is it necessary to read all the data and write it into the file again? Share to: "123 ",?? 'Title2' => "123 ",?? 'Title3' => "123 ",??......?? Infinite) at this moment, I will... 'data-pics = ''>
------ Solution --------------------
$ Array ['tle2'] = 456;
------ Solution --------------------
Oh, wrong. it was originally written into the file.
There are two solutions
1. if the file is large, read it in fgets and line by line until title2 is read. but there is a disadvantage, that is, the annotation problem.
2, file_get_content and file_put_content
------ Solution --------------------
Yes! Need to write again
Therefore, a large amount of data application databases
------ Solution --------------------
If the file is huge, you can change it in the #2 solution. write the new file as needed after each row is read.
Delete the old file (or change its name to bak) and rename the new file to the original one.
The premise is that the permission is sufficient.
------ Solution --------------------
Why should I read and write?
The main figure is the config. php file, which is clearly read.
Therefore, you only need to use var_export to construct the modified array into php code and write it back.
------ Solution --------------------
I am talking about the huge file size. if config is estimated to be not huge,
Reference:
Why should I read and write?
The main figure is the config. php file, which is clearly read.
Therefore, you only need to use var_export to construct the modified array into php code and write it back.
------ Solution --------------------
$ Handle = fopen ("config. php", "r + ");
$ Offset = 0;
If ($ handle ){
While (! Feof ($ handle )){
$ Buffer = fgets ($ handle); // The default value is 1024.
$ Offset + = strlen ($ buffer );
If (false! = Strpos ($ buffer, 'string to be replaced ')){
Fwrite ($ handle, 'Your content // '); comment out the following content ^. ^
Break;
}
}
Fclose ($ handle );
}
?>
The pointer may be incorrect. you need to debug it!
------ Solution --------------------
Let's change your mind,
When you encounter such a problem, consider whether the direction is wrong.