Re: variable placeholders in a text file

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

 



On 12-12-31 03:37 PM, Nelson Green 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.
  		 	   		
The reason you get $user_name printed is because of the way you are populating the variable $message. You need to have $user_name embedded in double quotes or a HEREDOC when PHP parses $messsage. And $user_name has to have already been defined.

Here is a sample from one of my sites. It is a simple one for the contact page.

=====contact.php====

<?php
$thispage = "Contact";
$contenttop = "<p>$thispage</p>";
$contentbody = <<<HEREDOC
<p>stephen@xxxxxxxxx</p>
HEREDOC;

require_once "include.php";
require_once "utilities.php";

echo $header . $markup;

=================

The common stuff for every page is defined in the file "include.php". I define the variable $markup in that file.

Here is the definition for $markup

$markup=<<<HEREDOC

<body>
  <div id="all">

    <div id="top">
      <img src=$titlepng alt=$title />
    </div>

    <div id="main">

      <div id="mainrow">

        <div id="left">
          $leftimage

          <div id="mainnav">

            <ul>
              $menu
            </ul>

          </div>

        </div>

        <div id="content">
          <div id="content-top">
            $contenttop
          </div>
          <div id="content-body">
            $contentbody
          </div>
          <div id="content-bottom">
          </div>
        </div>

      </div>
    </div>

    <div id="footer">
      $copyright
    </div>

  </div>

</body>
</html>
HEREDOC;

There is lots more code, but this is the important stuff.

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


--
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