Re: help with html through php

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

 



Rens Admiraal wrote:
Hi Todd,

Maybe I can show you a technique which is real helpful to me...

You can make a HTML file which you use as template, and add some standard strings (character chains) to it... Like [TIME], or [ACTIVE_USER]... When you open the HTML document in your browser, you can see where your time and active user have to appear. All you now have to do is find a way to replace those strings... When I hear the words replace and strings together I have to think about the str_replace() function... But, str_replace needs a string as haysteck, so you have to get the source of your HTML in a string... How to do that?

That's easy... You can read a file line for line with the file() function, this returns an array... but, an array isn't what you want, you want 1 string... so you use the implode() function to make it 1 string
This string you can use as haysteck, and echo it later...


I think it may be a little much info, so, here an example:

*template.html
*<html>
   <head>
      <title>[TITLE]</title>
   </head>
   <body>
      [BODY]
   </body>
</html>

*script.php*

<?php
$template_string = implode(" ", file ("template.html") );
$template_string = str_replace ("[TITLE]", "This is a script using templates", $template_string);
$template_string = str_replace ("[BODY]", "Body text", $template_string);
echo $template_string;
?>


The shorter way for Script.php if you have amny strings to replace is:

<?php
   $template_string = implode(" ", file ("template.html") );
   $replaces = array ("[TITLE]"      => "this is a script using templates",
                                  "[BODY]"      => "Body text");
   foreach ($replaces as $string => $value)
   {
      $template_string = str_replace ($string, $value, $string_template);
   }

   echo $template_string;
?>

easier/shorter would be:
<?php
$template_string = implode("\n", file('template.file'));
$replaces = array('[TITLE]'=>'a title', '[BODY]'=>'something else');
echo str_replace(array_keys($replaces), array_values($replaces), $template_string); // (array_values() isn't really required here)
?>
Raditha Dissanayake wrote:

Todd Alexander wrote:

Hello all,
I am a complete newbie to php so apologies for what I'm sure is a
simple/dumb question. I want to use php to include another html doc in
an existing set of documents. In other words "123.html" will use php to
call on "abc.html" so I can make changes to abc.html and have it update
on all my pages.

You have struck upon a pretty standard technique you will need to look at the include() or require() functions to persue this further. There is a subtle difference between the two but when you are getting started the difference does not really matter all that much. On most webservers you will need to name your 123 file not as 123.html but as 123.php or 123.phtml abc.html can continue to be abc.html



Again I realize this is a complete newbie question but I appreciate any help you have.

Todd






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