Use a .html file and use variables
Say i have a .html file that contains a web design and has variables in it like $time $comment $title.
how would i enter them variables from a php file
Sorry i c开发者_开发问答annot really explain it well enough but i hope somebody understands
Well.. I'll just guess what you want. Since the question isn't very clear.
In a PHP script, load the HTML in with something like file_get_contents, then use str_replace to replace the 'variables' in the HTML, then echo the new HTML.
Or, don't use an HTML file, use a PHP file, and just use echo where you want the variables to be.
Or... use something like smarty templates which is probably more advanced than you're looking for.
Learn PHP
Learn an MVC Framework of your choice
Change the .html file into .php
Put it in with your "Views"
Call it from your Controller (defining your view variables)
You can get apache to parse .html files as PHP by adding this directive to your .htaccess (or httpd.conf):
AddType application/x-httpd-php .htm .html
It might be necessary to change this line in your httpd.conf:
AllowOverride None
to:
AllowOverride All
That will enable .htaccess overrides to the apache configuration.
This answer assumes two things:
1 - That I understood the question.
2 - That you are in fact using Apache web server.
I also take a chance that I understand you right. If you think of a template system for PHP, I recommend you Smarty, which makes it very easy to divide HTML from php
Why would I use Smarty (when PHP is a template engine)?
Some might argue that Smarty does what PHP can do already: separate the presentation from business logic. The PHP programming language is great for code development but when mixed with HTML, the syntax of PHP statements can be a mess to manage. Smarty makes up for this by insulating PHP from the presentation with a much simpler tag-based syntax. The tags reveal application content, enforcing a clean separation from PHP (application) code. No PHP knowledge is required to manage Smarty templates.
This sounds more like a utilization problem. As others have noted, static html files are just returned as such, static files. If you want the variables replaced whenever those files are accessed, then define a handler like that from your .htaccess
:
RewriteRule (.+\.html) vars.php?file=$1
And a template function (vars.php
) to works on those placeholders:
<?php
// define all placeholder variables here
$html = file_get_contents(basename($_GET["file"]));
print preg_replace('/\$(\w+)/e', '$GLOBALS["$1"]', $html);
?>
But it's certainly easier to just rename your .html files into .php, and put a heredoc around them:
<?php
echo<<<END
<html>$vars</html> ...
END;
?>
精彩评论