RE: [PHP] Re: replacing variables in file

From: Date: Sun, 12 Aug 2001 03:50:16 +0000
Subject: RE: [PHP] Re: replacing variables in file
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-62287@lists.php.net to get a copy of this message
I can't quite understand exactly what you want to do... Replace what varaibles with what data? If you're saying you want to include HTML code with blanks in it, and have the PHP fill in the blanks with variables, that's easy... file1.php: [html stuff] <B><?php echo $variable;?></B> [html stuff] ......... file2.php: <?php [php code] include("file1.php"); [php code] ?> Or-- If you're saying you want to store the contents of a file in a variable, then do this: // get contents of a file into a string $filename = "/usr/local/something.txt"; $fd = fopen ($filename, "r"); $contents = fread ($fd, filesize ($filename)); fclose ($fd); (taken from PHP manual entry for fread()) Also see file() -- that performs a similar function, reading the file into an array. -----Original Message----- From: Paul Roberts [mailto:roberts_paul@bigfoot.com] Sent: Saturday, August 11, 2001 10:21 PM To: php-general@lists.php.net Subject: Re: [PHP] Re: replacing variables in file I want to read the include file in, replace the variables with any data present and then send the included file to the browser between some header and footer templates. I want the template to be straight html which can be edited with a wysiwyg editor if required. if I include the file as html I get errors as there are " and no valid syntax I my php block ie no ; i'd like to just do $formdata = (include($filelocation)); but this returns 1 and pts it in the variable, I need the html form in the variable. ----- Original Message ----- From: "CC Zona" <cczona@nospam.invalid> To: <php-general@lists.php.net> Sent: Sunday, August 12, 2001 1:35 AM Subject: Re: [PHP] Re: replacing variables in file | In article <000001c122b9$e055d460$7763883e@portable1>, | roberts_paul@bigfoot.com (Paul Roberts) wrote: | | > | $template="/path/to/somefile.inc"; | > | include($template); | > | | > | The variable substitution happens automatically (as long as $var occurs | > | within proper PHP syntax, such as "<?php $myvar1 ?>"). Output to browser | > | is also automatic. See <http://php.net/include> for more info. | > | | > | > I tried this | > print (include($filelocation)); returns true and prints 1 | | Like I said before: "See <http://php.net/include> for more info." | There's a wealth of useful info there (for example, note that if the | included file returns a value, that value can be captured into a | variable, like "$myvar=include($filelocation);"). If there's | something in those docs that | you need clarification on, do post again. | | > print (include($filelocation)); returns true and prints 1 | > | > include($filelocation)); causes the template file to be dumped into | > the middle of the script causing errors. | | Perhaps if you explained what differences you expected to get from | these two lines...? (Looks to me like you *want* to dump the template | file right | at that point. What errors specifically is PHP reporting?) | | > if I could get to work as in Perl I'd be happy, is there an | > equivalent of $1 | > in php | > | > foreach $item (@template) { | > $item =~s/\$(\w+)/${$1}/g; | | See <http://php.net/foreach> and <http://php.net/preg-replace>. | | -- | CC | | -- | PHP General Mailing List (http://www.php.net/) | To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net | For additional commands, e-mail: php-general-help@lists.php.net To | contact the list administrators, e-mail: php-list-admin@lists.php.net | | -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net For additional commands, e-mail: php-general-help@lists.php.net To contact the list administrators, e-mail: php-list-admin@lists.php.net

« previous php.general (#62287) next »