RE: [PHP] variable placeholders in a text file

2013-01-07 Thread Nelson Green

On Sat, 5 Jan 2013 04:20:09 -0600, Kevin Kinsey wrote:

 Nelson (et al),

 I've enjoyed reading this thread and apologize for dredging it up.
 It's interesting to see your progression of thought and the templating
 discussion is indeed a worthy one.

 However, I wanted to answer this objection from your initial message:

 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.

 You *don't* have to; str_replace() is perfectly capable of handling
 arrays:

 =
 $replace = array(USER,SITENAME,SOME_CONSTANT);
 $replacements = array($user,$site_name,$foo);

 $replaced = 
 str_replace($replace,$replacements,file_get_contents(/somefile.txt));
 =

 This, of course, doesn't negate a good templating system* ... but it's
 handy to know and you'll probably use it sooner or later.

 Kevin Kinsey

 P.S. *assuming that's not an oxymoron!

Kevin,

No apologies necessary, at least not to me. I always appreciate help and
I probably wouldn't have figured out your way on my own, at least not in
relation to what I am trying to do. And you've just proved once again that
there is almost always more than one way to accomplish a goal. I like this
solution as much as what I ended up with and will keep it handy.

Thanks!
  
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] variable placeholders in a text file

2013-01-01 Thread Nelson Green

 While using the *_once works in many cases, if you're doing a mass
 mailing kind of thing, you want to use the standard include/require so
 you can re-include it as your variables change:

 foreach ($customers as $customer) {
 $fullname = $customer['fullname'];
 $address = $customer['address'];
 // and so on
 include(mailing.php);
 process($mailing,$customer);
 }

 where mailing.php defines the $mailing variable as the content that
 got included and substituted.

 (Back before I came up with this, I was starting off in the same place
 as the OP -- and then I just realized wait --- PHP *IS* a templating
 system! a voila)

For what I am trying to do here, I think what I'm doing will suffice. Like you
did, I am going through the learning process. What I've read about templating
has whetted my appetite for bigger and better things, but I should probably
finish this first. Right now I'm just trying to personalize a rather dry
page just a bit.

Thanks!
  
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] variable placeholders in a text file

2012-12-31 Thread Nelson Green

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

RE: [PHP] variable placeholders in a text file

2012-12-31 Thread Nelson Green

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.
  
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] variable placeholders in a text file

2012-12-31 Thread Nelson Green

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.
  
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] variable placeholders in a text file

2012-12-31 Thread Nelson Green

On 2012-12-31, at 4:58 PM, tamouse mailing lists tamouse.li...@gmail.com 
wrote:
 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 sounds like it might be what I'm looking for. If I'm understanding
correctly, you are saying to use the include function to read in my
greeting file. I think I've got the basic gist of the concept, and will see
what I can hobble together real quick. This thought had not occurred
to me.

Thanks!
  
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] SOLVED: variable placeholders in a text file

2012-12-31 Thread Nelson Green

On Mon, 31 Dec 2012 1On Mon, 31 Dec 2012 17:47:00 Stephen D wrote:

 By using require_once instead of fopen and fread, I have simpler code
 and PHP evaluates the embedded variables in $markup without any need to
 use string functions.

 In your case, I would make the file greeter.php

 =greeter.php===
 $message = Hello $user_name. How are you today?
 ===

 You replace the fopen and fread stuff with a require_once function and
 $message gets included and the user name resolved all in one line of code.

 Hope this helps

 --
 Stephen

Aha! Thanks for the more detailed explanation. I totally misunderstood your
first reply. I understood why I was seeing $user_name instead of Nelson,
but I did not understand what you were trying to tell me.

My greeter file is now:
?php
$message=HEREDOC
Hello $user_name.
How are you today?
HEREDOC;
?

And my function is simply:
function print_greeting($user_name)
{
   require_once GREETER_FILE;
   print($message);
}

Now when I call the function, passing the string Nelson, it is printing
exactly as I wanted. I used a HEREDOC in the GREETER_FILE because I want to
make it more than that one message, and I want to preserve the line breaks
and formatting when I do.

From what I've been reading based on other replies, I think templating
will be the way for me to go at some point. I am trying to design a fairly
simple site that will be used internally as a front-end to a simple inventory
database that I've designed. The back-end works fine, but command line result
sets don't appeal to others, so I've embarked on a front-end design. Your
help has gotten me over this step and I can start to expand things. I find
I learn best when I start with some basic functionality and build it up. My
first incarnation was to simply build a static page that output exactly what
I put into it. Once I got that going, I then broke parts off into functions,
and then added parameter passing to the functions. I've already explained
where it went from there to here.

Not to detract from the other replies, but you have lead me to exactly what
I was looking for at the moment. I find smarty (and now a couple of others
picked up from reading about smarty) to be fascinating stuff, but I'm nowhere
near that point yet.

Thanks a million!
Nelson
  
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php