I have HTML content in the variable $detail and I want to "embed" PHP files there.
Note: The variable "$detail" gets stores the information from a query to the database.
This variable $detail has paragraphs "<p> </p>" in those paragraphs I need to be able to "embed" PHP files, for example, in the second paragraph, embed "file1.php" the other file in the fifth paragraph.
It is important that it is in PHP
I had already done something similar but with JAVASCRIPT but when the browser has JAVASCRIPT debugging deactivated everything is out of order, that's why I'm looking for some way to use it with PHP
Example:
include 'file1.php';
include 'file2.php';
//They must be included or embedded in the $detail variable, the first file, in the third paragraph, and the second file, in the seventh paragraph
echo $detail;
You can use output buffering to get file1.php and file2.php, additionally with preg_replace() like:
$detail = "
<p>1</p>
<p>...</p>
<p>3</p>
<p>4</p>
<p>...</p>
<p>6</p>";
ob_start();
include 'file1.php';
$file1 = ob_get_clean();
ob_start();
include 'file2.php';
$file2 = ob_get_clean();
$detail = preg_replace('/^(.*<p>.*<p>).*(<\/p>.*<p>.*<p>.*<p>).*(<\/p>.*)$/Us', "\${1}{$file1}\${2}{$file2}\${3}", $detail);
echo $detail;
However you should check your business logic or use a PHP template engine, like Smarty as #kiko-software said earlier.
Related
I have a kind of template file that may be called filename.php:
<h1>
<?= $test . ' world'; ?>
</h1>
<p>
Some text
</p>
Then I have a function in an index.php file that looks like this?
<?php
function test($args) {
$test = $args;
include 'filename.php';
}
test('Hello');
test('Hello');
test('Hello');
This code works. It output the included data 3 times.
I can also use output buffering to get the output as a string if I want. However, that's not exactly what I want.
Problem
I can't figure out a way to only need to include the filename.php one time (now it's loaded 3 times). Because it accepts arguments it can't be returned as string. It needs to be returned as an anonymous function, I guess. Then I could buffer my template and still use it with new values.
Any creative ideas are welcome.
I have two files index.php and template.html. In the template file I have a div, which contains some PHP code inside. What I am trying to achieve is to pull the div from template including everything inside and insert it to my main index page. I managed to do so, but only if there is no PHP code inside the div. If however there is any PHP included I see something like this "saveHTML($snippet[1]) ?>;" instead of full PHP code block. Could you please explain the reason why I am not able to move the div including PHP codes.
index.php file
<?php
//some basic stuff such as new DOMDocument(); loadHTMLFile and so on
$post = $posts->query("//div[contains(#class, 'post')]");
?>
<body>
<?php echo $templates->saveHTML($post[0]);?>
</body>
template.html file
<div class="post">
<?php echo $examples->saveHTML($snippet[1]) ?>;
</div>
you can do it in a simpler manner
first in template.html please replace your dynamic content with %%posts%%
i.e,
template.html
<div class="post">
%%posts%%
</div>
then in your index.php get contents of template using file_get_contents
after that replace it with your dynamic code like as below
$htmlFile = 'template.html';
$yourDynamicContents = 'Replace your dynamice msg here';
$contents = file_get_contents($htmlFile);
$contents = str_replace('%%posts%%', $yourDynamicContents, $contents);
$contents has whole page...you can either echo or send mail with that template or even pass to print pdf etc
the above will do it simply and clean.
P.S you can replace anything in $yourDynamicContents whether its css,html,js
Hope the above answer helps
Thank you
Have you tried getting the content of your template file via file_get_contents? Then maybe you can extract your div with substr() using strpos()
My goal is to have a file with the following html/php created on the server automatically
which it does do, however it includes the php includes as well so when the page is build it includes everything that is in the include as well.
I would want include("../includes/right.html"); to be left alone
in the page that is generated,but I want other php variables to be parsed.
<?php
// Start the buffering //
ob_start();
?>
<?php echo $name; ?>
test text line one
<?php include("../includes/right.html"); ?>
test text line two
<?php
// Putting content buffer into file //
file_put_contents('mypage.html', ob_get_contents());
?>
This is straight from the PHP manual at http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.nowdoc
This is called nowdoc syntax and comes with PHP 5.3. It's generally the best practice for something like this.
echo <<<'EOT'
My name is "$name". I am printing some $foo->foo.
Now, I am printing some {$foo->bar[1]}.
This should not print a capital 'A': \x41
EOT;
anything between 'EOT' and EOT; will be rendered on the page as text with no parsing done whatsoever.
You need to escape that line as HTML, so it's not passed through the PHP parser, e.g.
<?php
// Start the buffering //
ob_start();
?>
<?php echo $name; ?>
test text line one
<?php include("../includes/right.html"); ?>
test text line two
<?php
// Putting content buffer into file //
file_put_contents('mypage.html', ob_get_contents());
?>
I've got a simple (but not tiny) template for some HTML, complete with inline variables. I'd like to pull that out as a separate file, and have the ability to switch in other template files. Is there a way to load a file into a string, but have it process inline variables?
Eg:
$thing="complete sentence";
$test=<<<END
This will get parsed as a $thing.
END;
echo $test; // This will get parsed as a complete sentence.
What I want is something like this:
// "test.html"
<html>
<body>
<p>This will get parsed as a $thing.</p>
</body>
// "index.php"
$thing="complete sentence";
$test=file_get_contents("test.html");
echo $test; // This will get parsed as a complete sentence.
How do I achieve this, preferably without a templating library?
<?php
$thing="complete sentence";
$test=file_get_contents("test.php");
echo preg_replace_callback('#\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)#','changeVariables',$test);
function changeVariables($matches)
{
return $GLOBALS[$matches[1]];
}
This code uses preg_replace_callback to check what is variable. But, because we are in function, we cannot directly access script variables. We have to use $_GLOBALS variable which contains every script variable. $matches[1] contains name of matched variable.
Something like this should work...
// "test.php"
This will get parsed as a %s.
// "index.php"
$thing="complete sentence";
$test=file_get_contents("test.php");
printf($test, $thing);
You can use include to simply load the file as if it were part of the calling code.
include("included_file.php");
If you cannot include for some reason, you can read the file contents and eval it.
$content = file_get_contents("included_file.php");
eval($content);
UPDATE:
As pointed by NikiC, your file test.html doesn't have valid PHP. You would have to change it so include can work. Your test.html should have this content:
<html>
<body>
<p>This will get parsed as a <?= $thing ?>.</p>
</body>
And eval would not work with this code, as this is not pure PHP code, it is HTML code with PHP inside it. If your included file has just PHP code, it would work fine.
I have a footer in a web page, the footer is called footer.php. I'd like the footer to have a different image depending on other variables. I want to do something like this:
if (x=1) {include (str_replace('logo1.jpg','logo2.jpg','footer.php'));}
else
{include 'footer.php';}
But this doesn't work, it just does a regular include. Is there a way to replace text in files while including them?
Use something like:
if (x==1) {
$image='logo1.jpg';
} else {
$image = 'logo2.jpg';
}
include('footer.php');
////footer.php
echo "<img src='".$image."'/>";
Basically, what you had would try to do the replace on the string 'footer.php', not the file itself. The appropriate approach here would be to use a variable for your image and have the footer use that variable when supplying the image.
Is there a way to replace text in files while including them?
Yes there is, but your included file would have to return its contents.
footer.php
<?
return "<img src='#image'>";
?>
then you can do
echo str_replace("#image", "image.jpg", include("footer.php"));
there's nothing wrong with this but it feels slightly weird, though.
If I were you, I would have the include() just work with a pre-set variable as Jonathan Fingland proposes, or fetch the contents of a footer file like Tomas Markauskas proposes.
Your example isn't working because you're replacing 'logo1.jpg' with 'logo2.jpg' in the string 'footer.php'. The result of the replacement is still 'footer.php' and then you're just including a file with the name that matches your string.
If you really need to replace a string in a php file and execute it afterwards, you could do something like this:
$file = file_get_contents('footer.php');
$file = str_replace('logo1.jpg', 'logo2.jpg', $file);
eval($file);
But there are better ways to achieve what you want (see answer from Jonathan Fingland for an example).