RE: [PHP] variable placeholders in a text file

From: Date: Mon, 31 Dec 2012 20:37:24 +0000
Subject: RE: [PHP] variable placeholders in a text file
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-319981@lists.php.net to get a copy of this message
On Mon, 31 Dec 2012 14:47:20 Stephen D wrote: > > Yes! > > Easy standard stuff. > > $title = 'Mr."; > $user_name = 'John Doe'; > > $message = "Hello $title $user_name ...." > > Just define the value for the variables before defining the value for > the message. > > Note that $message has to use double quotes for the expansion. Also > consider using HEREDOC instead of the double quotes. > > You may want to put your message in a text file and using the include > function. Hi Stephen, My message is in a text file, but I'm using fopen and fread in a self-defined function, so message is actually defined as (GREETER_FILE is a defined constant): function print_greeting($user_name) {    $handle   = fopen(GREETER_FILE, "r");    $message  = fread($file_handle, filesize(GREETER_FILE));    $msg_text = str_replace("USER", $user_name, $message);       print($msg_txt); } And my text file is simply: $cat greet.txt Hello USER. How are you today? If I change USER to $user_name in the text file and change the print function parameter to $message, $user_name gets printed verbatim. In other words the greeting on my page becomes: Hello $user_name. How are you today? I want to pass the name Nelson to the function, and have it output: Hello Nelson. How are you today? after the function reads in text file input that contains a variable placeholder for the user name. I actually had a HEREDOC in the function, and that worked. But by reading a file instead, I can make things more flexible. I'd rather be changing a text file instead of a code file.

« previous php.general (#319981) next »