php array and html
OK i have 2 files one is index.php and the other is file.html now i want to display my array content through that html file li开发者_运维问答ke calling it and then displaying here is the code
index.php:
$var1 = 'text';
array('var' => $var1);
include ('file.html');
file.html:
<p>html {var} html</p>
now its not displaying text i have seen people use {} and call the key and display its value how do i do that? all i want is display my values through the html file not with echo or using php open close codes in between the html codes.
If you want to replace tags {name}
to value with $a = array('name' => 'value');
you can use str_replace();
$filehtml = file_get_contents('file.html');
$array = array('var' => 'value');
foreach($array as $tag => $value)
{
$filehtml = str_replace('{'.$tag.'}', $value, $filehtml);
}
echo $filehtml;
Or you use Smarty (or others).
you must use template engines like smarty, or PatTemplate. or write your own.
PHP doens't do anything with {var} statement. You should work with it manually... or read about template engines http://www.smarty.net/ for e.g.
What you saw I assume were templates of some template engine or other. When using templates, the (HTML) template files are parsed on runtime and the placeholders ({var}) are replaced with their values. If you google around for PHP template engine a bit I'm sure you'll get the idea of it and/or find an existing solution to use.
It sounds like you want to make it easier to work with HTML without having to have PHP code embedded in it. This would be done using a templating language.
There's a number of templating languages available for PHP; the most well-known one is Smarty.
However, I would caution you that if you have any kind of complexity in your pages, you will need some sort of program code. Smarty and others allow you to do loops and conditions, but when used like this, it can very quickly become a programming language in its own right. Plus they add quite a bit of processing overhead, which is really unnecessary.
The bottom line is that for most uses, it's easier just to stick with standard PHP tags for printing your variables into your HTML code.
See also a previous answer I gave to a similar question: Dirt-simple PHP templates... can this work without `eval`?
You could use extract(), just be mindful of collisions!
index.php
$var1 = 'text';
$variabbles = array('var' => $var1);
extract ($variables);
include ('file.html');
file.html:
<p>html <?php echo $var; ?> html</p>
This is definitely not the way of templating but here's a rough snippet, so you get the idea.
$var1 = 'text';
$tags = array('var' => $var1);
$tpl = file_get_contents('file.html');
foreach($tags as $label => $data) {
$tpl = str_replace('{' . $label . '}', $data, $tpl);
}
echo $tpl;
You can use output buffering to solve your problem but be careful on quotes if you only use php to output it.
If you don't want to use a template engine for this task, I could suggest the following code to add to your index.php:
<?php
$var1 = 'text';
$templateVars = array('var' => $var1);
$content = file_get_contents('file.html');
foreach($templateVars as $tplVar => $tplVarContent) {
$content = preg_replace('/\{' . $tplVar . '\}/', $tplVarContent, $content);
}
echo $content;
Why using preg_replace()
?
It's known that preg_replace()
does a better job at replacing in a bigger text content rather than str_replace()
which starts to be slower and slower after each replacement (you can find a benchmark on Google, I can't seem to find it right now)
Later edit:
I found am improvement on this one.
<?php
$var1 = 'test';
$templateVars = array('var' => $var1);
$content = file_get_contents('tpl.tpl');
$content = preg_replace('/\{([a-zA-Z0-9_]+)\}/e', "\$templateVars['\\1']", $content);
echo $content;
I have also done a little test against the str_replace()
solution. str_replace()
is faster only when there are a couple of variables (2-3). With 6 variables preg_replace()
is 2 times faster.
Here are my results:
preg_replace() 10000 times: 0.038811922073364seconds
str_replace() 10000 times: 0.085460901260376seconds
This is the code I ran to test the speed:
<?php
$var1 = 'text';
$templateVars = array('var' => $var1, 'var1' => 'blabla', 'var3' => 'variable3', 'var22' => $var1, 'var133' => 'blabla', 'var3444' => 'variable3');
$content = file_get_contents('tpl.tpl');
$start = microtime(true);
for($i = 0; $i <= 9999; $i++)
{
$content = preg_replace('/\{([a-zA-Z0-9_]+)\}/e', "\$templateVars['\\1']", $content);
}
$end = microtime(true);
echo 'preg_replace() '. $i . ' times: ' . ($end - $start) . 'seconds<br>';
$start = microtime(true);
for($i = 0; $i <= 9999; $i++)
{
foreach($templateVars as $tplVar => $tplVarContent) {
$content = str_replace('{' . $tplVar . '}', $tplVarContent, $content);
}
}
$end = microtime(true);
echo 'str_replace() '. $i . ' times: ' . ($end - $start) . 'seconds<br>';
PHP doesn't care what file types you include - it will execute them as if they were php files. So.....
ndex.php:
$var1 = 'text';
$myArr = array('var' => $var1);
include ('file.html');
file.html:
<p>html <?php print_r($myArr);?> html</p>
精彩评论