aQ Posted July 22, 2007 Share Posted July 22, 2007 Hello! I am experimenting a little using Flat files now. I have a file that looks something like this: <?php // variable $var = "asdasd"; // array $array = array("lala", "lalal"); ?> How can I replace the $array's content with for example "array("blablabla", "blabla")" without knowing the old value of it? Quote Link to comment Share on other sites More sharing options...
BillyBoB Posted July 22, 2007 Share Posted July 22, 2007 from my understanding of your question: <?php // variable $var = "asdasd"; // array $array = array("lala", "lalal"); $array[1] = $var; echo $array; ?> try taht Quote Link to comment Share on other sites More sharing options...
aQ Posted July 22, 2007 Author Share Posted July 22, 2007 Uhm, I think I was a little unclear. I want to use fwrite to change the values of the array, and let the variable and such stay the same. Thanks. Quote Link to comment Share on other sites More sharing options...
BillyBoB Posted July 22, 2007 Share Posted July 22, 2007 still unclear do you want to edit another file in which the array is in that fwrite... Quote Link to comment Share on other sites More sharing options...
aQ Posted July 22, 2007 Author Share Posted July 22, 2007 I have a file with a form, the form is posted to replace the values in the file with the array. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 22, 2007 Share Posted July 22, 2007 I think what you're after is something like: <?php //$file = file_get_contents('yourfile'); $file = '<?php // variable $var = "asdasd"; // array $array = array("lala", "lalal"); ?>'; echo '<textarea rows=10 cols=30>'.$file.'</textarea>'; $array_to_replace = "array";//the name of the array you are wanting to replace $replace_with = '$'.$array_to_replace.'= array("blabla","blahaha");';//whatever you want to replace with $file = preg_replace('|\$'.$array_to_replace.'(.*?);|s',$replace_with,$file); echo '<textarea rows=10 cols=30>'.$file.'</textarea>'; //write new file with $file; ?> As you can see, i tested it with a normal string - but if you use file_get_contents you can read the file into a string anyway, so just modify that bit. And then you'll have to write back to the file. Quote Link to comment Share on other sites More sharing options...
aQ Posted July 23, 2007 Author Share Posted July 23, 2007 Hey, that's great. Works exactly as I want! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.