RE: [PHP] Changing variables in a text file

From: Date: Wed, 26 Mar 2003 01:13:45 +0000
Subject: RE: [PHP] Changing variables in a text file
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-141046@lists.php.net to get a copy of this message
> I have a text file with a series of project variables. > > For example > $var1="xxxx"; > $var2="aaaa"; > $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/

« previous php.general (#141046) next »