I have encountered a problem, but I cannot find it.

Source: Internet
Author: User
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.

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.