How to get the rendered PHP+HTML codes into a text file? - php

I have a file which is combination of PHP and HTML.
How can i get the plain HTML code output from this file into a text file? or either put it into the text box.
The code of my form, which i use to generate an HTML form can be found here

Update:
It seems OP is asking about HOW to get the rendered PHP+HTML codes into a text file?
//get the output from the file
ob_start();
include "yourphpplushtml.php";
$output = ob_get_clean();
//now create a text file and write to it
$fp = fopen('data.txt', 'w+');
fwrite($fp, $output); //put the output
fclose($fp); //close the handler
//Or put it into the textare
echo '<textarea>'.$output.'</textarea>';
Previous Answer But may be helpful to others too
There are many ways HTML can be combined with PHP
Output HTML directly from PHP
<?php
echo "<head><title>my new title</title></head>";
?>
Include PHP inside you HTML
<title><?php echo $dynamictitle; ?></title>
Or even separate them in old fashioned complicated way
<?php if($resultFound == true) { ?>
<p> The result was successfully found.</p>
<?php } ?>

Related

How can I stop html outputting?

I am writing an anti-ddos php script, and I want to write a custom load page, but the html page of the orginal html page would be rendered. Is there anyway for me to prevent the html showing up while showing up my php load page
<?php require "antiddos.php"?>
#Samuel Wang, you can try check that out by one of this examples.
below assume code of antiddos.php and other file from where you include it.
1. antiddos.php :
E.g. before your php code or you can keep html inside the file that won't be an issue.
<?php
echo"<p>This is a test html content of antiddos.php</p>";
function testcall($TextReceiver ){
return $TextReceiver;
}
function MathMultiplication($Prt1,$Prt2){
return $Prt1*$Prt2;
}
?>
2. testprocess.php :
You can use the ob_clean(); function next to your include file statement that will discards the contents / html output from the antiddos.php.
<?php require_once("antiddos.php");ob_clean();
echo testcall("Hello this is print call from testprocess.php to antiddos.php");
echo "<br>";
echo "MathMultiplication= ". MathMultiplication(4,3);
?>
Conclusion by both the files output: First from the antiddos.php and second one from the testprocess.php

How to printf an include html

I included an html file like this, so that it is not displayed when the site loads:
<div id="menugrp0" class="menuhide">
<?php include 'menugrp0.html'; ?>
</div>
Now I want it to be shown at a specific spot. I am using this php code, to get some variables which are transported with the $_SESSION. I am using this kind of question for some simple html links, in which case it works perfectly:
if ($_SESSION['gruppe'] == $h['gruppe']) {
printf(' menugrp0.html');
}
I know that this is not working at all at the moment for this included html. I also tried to add the <?php [...] ?> tag inside the printf, which is also not working.
Is it possible to show a hidden included html file with a printf tag?
Try this one.
<?php
if($_SESSION['gruppe'] == $h['gruppe']){
echo 'Foo';
include ('/path/to/menugrp0.html');
echo 'Example: one';
}
?>
readfile('menugrp0.html'); // Reads a file and writes it to the output buffer. It is like read then "echo"
How to echo the whole content of an .html file in php?
Thanks to Daan Meijer, this works.
echo file_get_contents('...');
}

Import HTML tag from external file which contains PHP code

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

PHP - How to echo a variable that contains php?

If I have a piece of code that reads a chunk of HTML from a txt file and then echos that html onto the page, how can I accomplish the same task, but when there is PHP inside of the txt file?
ex:
this is the file being read:
<?php
$filecontent = // read some other file
echo($filecontent);
?>
and this is the page that is reading the file:
<?php
$code1 = //reading the above file
?>
<html>
<?php echo($code1); ?>
</html>
When you want to process files containing PHP code you need to use include instead of echo.
<?php include('your_php_file_name'); ?>
If you have the contents of the file in a string you are in a tough spot because the only way to process the code is eval, and in addition you have to properly set up any environment that the code requires. eval itself should be avoided, and the latter is impossible to do in the general case.
Use include instead of echo:
<?php include($file_that_contains_php); ?>
you need to include the first file and echo statement in the first file will get executed.
<html>
<?php require_once("firstfile.php"); ?>
You need to echo htmlentities($code1), because when you echo then browser will not show it contents, because it try to parse it as a html tag, but htmlentities will encode to safe html output this characters.
If you want to evaulate the code, then you need eval($code1) or include it.

Php file that does not parse includes

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());
?>

Categories