Re: variable placeholders in a text file

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




Bastien Koert

On 2012-12-31, at 4:58 PM, tamouse mailing lists <tamouse.lists@xxxxxxxxx> wrote:

> On Mon, Dec 31, 2012 at 2:37 PM, Nelson Green <nelsongreen84@xxxxxxxxxxx> wrote:
>> 
>> 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.
> 
> I use the include("template") method for this alla time, it works
> great. Most especially for HTML emails coming from a web site to a
> group of users, just slick as anything. include does basically just
> what your print_greeting function does less the actual printout, but
> using php variables instead of a str_replace. Also, this way the
> templates can be stored elsewhere, outside the actual code base if
> need be.
> 

This is exactly what I do. Dead simple fast and the templates are fully self contained.
-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux