[PHP] Changing variables in a text file
I have a text file with a series of project variables. For example $var1=; $var2=; $template=blue; $anothervar=foo; Let say I need to change $template=blue; to $template=red;. How can I do that keeping the rest of the file intact? thanks, Luis
Re: [PHP] Changing variables in a text file
If you know what is suposed to be in the file you can simply build up a new file. If you don't know what is in there but only that you need to change this to that, use file functions and regexes. You might also consider using an array instead of plain variables, so your file will become $config['var1']=; $config['var2']=; $config['template']=blue; $config['anothervar']=foo; This will make updates easier, as you know you only need to write everything in $config. Luis Lebron wrote: I have a text file with a series of project variables. For example $var1=; $var2=; $template=blue; $anothervar=foo; Let say I need to change $template=blue; to $template=red;. How can I do that keeping the rest of the file intact? thanks, Luis -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Changing variables in a text file
I have a text file with a series of project variables. For example $var1=; $var2=; $template=blue; $anothervar=foo; Let say I need to change $template=blue; to $template=red;. How can I do that keeping the rest of the file intact? You have to write the whole file over. So read the entire file into a string. If you know you're looking to match $template, then you can use a preg_replace() call. $new_value = red; preg_replace('/\$template=[^];/','$template='.$new_value.';',$text) ; or something similar. Adapt to your needs. ---John W. Holmes... PHP Architect - A monthly magazine for PHP Professionals. Get your copy today. http://www.phparch.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php