-1

I have a .php file that contains array data like below:

$config['setting_one'] = true;
$config['setting_two'] = "some string";
$config['setting_three']['option1'] = "some string";

What is the correct way to find the line starts with $config['setting_two'] and replace the line with $config['setting_two'] = "new string"; (new string will come from a post value).

Kanishka Panamaldeniya
  • 17,302
  • 31
  • 123
  • 193
  • You have a missing quote on one of your lines: `$config['setting_three']['option1'] = "some string"` – uınbɐɥs Aug 14 '12 at 00:15
  • 1
    -1 for reposting and ignoring the advise given. And those new answers here aren't useful references for any future user. – mario Aug 14 '12 at 01:04
  • possible duplicate of [Finding a specific line in php with and replace the line with preg_replace?](http://stackoverflow.com/questions/11921136/finding-a-specific-line-in-php-with-and-replace-the-line-with-preg-replace) – mario Aug 14 '12 at 01:05
  • @mario, it is not fair to give -1 rep before check it up. I deleted the old post before posting the new one. I just make a new post because I wanted to display he content of php file I am dealing with. Is it crime to look for better advise by writing a more proper question for it? –  Aug 18 '12 at 13:19
  • @Shaquin Trifonoff, thank you. –  Aug 18 '12 at 13:20
  • It's not fair either to delete and disregard the answers you already received. If you're unhappy with the state of your last question, you can (1) add additional information, (2) add a bounty. Reposting a *shortened* version is **not** acceptable. – mario Aug 18 '12 at 13:58
  • possible duplicate of [Editing PHP files for configuration via PHP](http://stackoverflow.com/questions/833237/editing-php-files-for-configuration-via-php) – mario Aug 18 '12 at 14:01
  • @mario, you are right. Sorry for inconvenience. –  Aug 18 '12 at 14:19
  • 1
    @mario I would like to fix downvotes. Please tell me how. Thanks for your help. –  Oct 02 '12 at 07:24

2 Answers2

4

There is no correct way.

The correct way is "don't do that".

You include the php code, and then update the variable by simply setting it.

What are you trying to do?

Ariel
  • 25,995
  • 5
  • 59
  • 69
  • I am building a web admin panel for a project, I want to change these config values after getting posted preference. –  Aug 18 '12 at 13:21
  • in example, when I get a post value tells what's the new value setting_two, I need to find this line in php file and replace it all with new value. –  Aug 18 '12 at 13:27
  • 1
    Is this file user editable? Because if so your parser needs to handle the full PHP grammar (which is more complicated). If not you could probably get away with search and replace on the array key name. A better way might be to generate the file from scratch every time. Do you need to display these values to the user so he can edit them? Because if so, and the user can edit the file they could put in some some code there that could mess things up. Answer my two questions and I'll give you a better answer. – Ariel Aug 19 '12 at 03:02
  • yes the file is editable. Users will post some given values or string and I will find the line in php file and replace its value. –  Aug 25 '12 at 20:54
  • 1
    @yahyaE OK, if the file is user editable then you MUST NOT try to replace the line. Instead, load the file in PHP using `include()`. Make whatever change you need to the `$config` variable. Then write out the file from scratch. But be VERY VERY careful! Since the user can edit the file they can put any code they want in there, including code to completely obliterate any security you have. – Ariel Aug 26 '12 at 03:20
1

You will have to read the file line by line using fgets

$config = array();

while(($line = fgets($handle, 4096) ) !== false) {
    $config[] = $line;
}
Ibu
  • 42,752
  • 13
  • 76
  • 103