Re: variable placeholders in a text file

From: Date: Mon, 31 Dec 2012 19:47:20 +0000
Subject: Re: variable placeholders in a text file
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-319977@lists.php.net to get a copy of this message
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. On 12-12-31 02:39 PM, Nelson Green wrote:
Hello, I have created a simple function that prints a personalized greeting by reading the greeting contents from a file. I pass the user's name to the function, and the function reads the file contents into a string variable. I am then using str_replace to replace the word USER in the string with the user's name that was passed to the function. Then the function correctly prints the personalized greeting as I wish. My question is, is there another way to do something similar, such as embedding a variable name directly into the text file? In other words, instead of my text file reading: Hello USER ... Can I do something like this: Hello $user_name ... and then write my function to replace $user_name with the passed parameter prior to printing? The reason I ask is because I am going to want to do three substitutions, and I'd rather not do three str_replace calls if I don't have to. Plus the latter seems to be a more robust way of making the changes. Thanks, and apologies if this has been asked before and I missed it. I'm just not sure how to phrase this for a search engine. Nelson
-- Stephen

« previous php.general (#319977) next »