RE: [PHP] variable placeholders in a text file
| From: | Nelson Green | Date: | Mon, 31 Dec 2012 20:29:38 +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-319980@lists.php.net to get a copy of this message | ||
On Mon, 31 Dec 2012 19:59:02, Ashley Sheridan wrote:
_______________________________
>
> On Mon, 2012-12-31 at 13:39 -0600, Nelson Green wrote:
> 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?
>
>
> You could use an existing templating solution, like Smarty, although
> for what you want to do it might be overkill. A few str_replace() calls
> shouldn't produce too much overhead, but it depends on the size of the
> text string in question.
>
> You could try eval() on the block of text, but if you do, be really
> careful about what text you're using. I wouldn't recommend this if
> you're using any text supplied by a user. As a last option, you could
> have the text stored as separate parts which you join together in one
> string later. This might be less expensive in terms of processing power
> required, but it also makes maintenance more of a hassle later.
>
>
> Thanks,
> Ash
> http://www.ashleysheridan.co.uk
Hi Ash,
Yep, smarty would be way more than I need right now. I'm just dabbling with
various things, trying to learn more about PHP. And my first thought was to
split the components, which worked fine. Then I tried a HEREDOC which did
allow variable substitution. This attempt is a move up from that, trying to
generalize things a bit more. My input will be 100% generated by me, but in the
back of my mind I'm looking towards the ability to use user supplied strings.