I am looking for a simple and effective way to create a pure html file based off a php file. For instance, in template.php below the php would be inserting various portions of the page. I want to save the page then as html removing all php code and leaving what was inserted by it... hopefully that makes sense... the output of template.php would be a better way to say it I guess.
First, I do not know if something like this is possible. Second, is this the best way to go about something like this?
Before anyone starts screaming about security there will be ZERO user submitted / form submitted variables in this page. My goal is to create a report from database values with the template which the user can then view/print/save off the server as pure html. There will be no images only inline css.
EDIT :
This html only output of template.php needs to be saved on the server as its own file. The reason for the php 'template' is because I will be creating the vast majority of the page with php... but I only want to save its resulting output.
template.php :
<!DOCTYPE html>
<html lang="en">
<!-- BEGIN HEAD -->
<head>
<meta charset="utf-8" />
<title><?php echo $title; ?></title>
<meta content="<?php echo $desc; ?>" name="description" />
</head>
<!-- END HEAD -->
<!-- BEGIN BODY -->
<body>
... further html with php mixed in
</body>
<!-- END BODY -->
</html>
Current solution :
I did some further research and this is acting exactly how I want it to. Comments/suggestions welcome for it.
<?php
ob_start();
require_once('/home/test/public_html/template.php');
if ( ob_get_length() > 0 )
{
$ssReport = ob_get_contents();
file_put_contents('/home/test/public_html/test.html', $ssReport);
}
ob_end_clean();
?>
You can use REGEX (Regular Expressions) to strip out all php-code.
The Expression <\?php.*?\?> can do that for you...
PHP-Example:
$tempateFile = file_get_contents('template.php');
$htmlPlain = preg_replace('/<\?php.*?\?>/si', '', $tempateFile);
If you allow "Shorthand Open Syntax" for php-files <? instead of <?php you should use the pattern <\?(?:php)?.*?\?> which will become preg_replace('/<\?(?:php)?.*?\?>/si', '', $tempateFile);
If you want to take a html-snapshot of your site(s) try this options:
use wget to grab a copy of your website (see wget manual or wget tutorial)
use curl with a script to grab a copy of your website (see curl manual or
curl tutorial
make a php-script which uses file_get_contents($url) to grab outputs of you specific public-available URL and store it with file_put_contents(...) to a html-file.
Related
I am a young developper (15 year)
I have a problem white my website. My page does not synchronize with html and php in "localhost/sn/". Normally when I type "localhost/sn/" in my browser I should have the html page synchronized with the php. But it doesn't work. When I type that, I only have my index.php page.
my server is with XAMPP.
Please help me!!!!!
If you want to run your website with only html and css structures, you use the file with the extension html (e.x index.html) and of course you do not need to use XAMPP.
If you want to run your website and have php code then use the file with the extension php (e.x index.php) and more importantly you need to put it in the htdocs directory of XAMPP.
There are several things you can do.
First, PHP is a programming language, where you can mix PHP code and HTML output together. There are several reasons, why you shouldn't (so called spaghetti code, see here), but as you are just starting, this seems to be ok.
On the other hand, you can integrate your HTML into your PHP with require or include.
Of course you are not bount to either the one ore the other. You can mix the two approaches (and every serious PHP app does it)
Mixing HTML and PHP
<?php
// We are in PHP country now
// Set the value of variable
$header = "My 1st header";
// This ends PHP country
?>
<!-- Now we are in HTML country -->
<h1><?= $header =></h1>
<?php
// PHP country again
// the character string `<?&= some_value ?>;`
// is a shortcut for `<?php echo some_value; ?>`
$count = 4;
for ($i = 0; $i < $count; $i++) {
echo "<p>Paragraph</p>";
}
This will output <h1>My 1st header</h><p>Paragraph</p><p>Paragraph</p><p>Paragraph</p><p>Paragraph</p>
Include HTML in PHP
Here we have two files
index.php:
<?php
require "header.html";
echo "<p>Content</p>";
header.html
<h1>My 2nd header</h1>
Calling index.php in XAMPP will yield <h1>My 2nd header</h1><p>Content</p>
You only need 1 file {index.php},
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>title</title>
<link rel="stylesheet" href="style.css">
<?php
echo ("Hello Arthur!");
?>
</head>
<body>
<div id="top">
asdfasdfsd
<div id="under">
asdfsadf
</div>
</div>
<!-- page content -->
</body>
</html>
then, open Chrome or whatever browser you like, type:
localhost/index.php
make sure the {index.php} file is in htdocs folder and xampp Apache is turned on.
hope I helped.
if you want just show html page then u do not need xampp. If you want to show html page with some logic then it have to be index.php with template in html
I've built a standalone php chat app. Works great with the classic localhost/chat, except I need it to run on an html page I already have. Does it have to be strictly html, the answer is no. The thing is, I know a browser can render html, but it can't render php.
I would just really like to know if there was any way for me to import a php app to a website. Chat app is consisted of 3 files, 2 of them are php which define the form, look and post method, the 3rd one is a log.html, which is used as a dumping place, that's how the app displays the messages,connected users, leaving users, etc.
I tried several different things.
-Tried using iframes
No good. It display raw php code
-Placed everything in www folder in wamp, tried to "require" the files in html page
No good, displays nothing.
There's really no code for me to show off, I've got everything working separately, except I need the php to work from inside my contact.html page.
It sounds like you simply don't understand HTML or PHP. You say that "The thing is, I know a browser can render html, but it can't render php." as if you're doing us a favor, when all it does is confuse us and then you attack us in the comments. A browser can 100% render a PHP file as long as it sends over HTML to the client, and seeing as you believe your PHP app works right now I'm not sure how you've tested it other than through the browser. Maybe you've used PHP from the command line but there'd be no graphical interface, and any HTML code (such as the form you say you've made) would simply be printed into the console where it should be obvious to you that a browser receiving the same thing would render it as HTML.
To prove this to you, just take a look at facebook:
https://www.facebook.com/ will direct you to the index.XXX within the root directory which is standard knowledge when working on sites.
https://www.facebook.com/index.html sends you to the error page as it doesn't exist (no html exists at index.html they just have the server configured to serve the error page any time a bad request comes in).
https://www.facebook.com/index.php works perfectly because that's the page that is normally chosen as the index.XXX and is rendered.
And finally, some demo code to prove my point:
<?php
$thingA = "dogs";
$thingB = "cats";
$finalThing = $thingA . $thingB;
?>
The above code would work perfectly fine, $finalThing would be "dogscats", and your server would take the output of the file (nothing) and send it as HTML.
<?php
$thingA = "dogs";
$thingB = "cats";
$finalThing = $thingA . $thingB;
echo $finalThing;
?>
The above code would do everything the first example did, but the output "dogscats" would be sent to the client as HTML. This would just render the text "dogscats" as if you had an HTML file with just those characters in it (no html tag, body tag, meta tags, or anything else at all).
<?php
$thingA = "dogs";
$thingB = "cats";
$finalThing = $thingA . $thingB;
echo "<!doctype html>";
echo "<html lang='en'>";
echo "<body>";
echo "\t<p> " . $finalThing . "</p>";
echo "</body>";
echo "</html>";
?>
The above would print a valid HTML file (without meta tags or many other useful things but sorry, I don't want to write much more and there's still another example) which should look exactly like the previous example, but with the text displayed on the screen being in a p tag rather than being raw text just sitting there.
<!doctype html>
<html lang="en">
<head>
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<meta charset='UTF-8'/>
<title>Website</title>
<script src='https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>
</head>
<body>
<?php
$thingA = "dogs";
$thingB = "cats";
$finalThing = $thingA . $thingB;
echo "\t<p> " . $finalThing . "</p>";
?>
</body>
</html>
And this final example is the best of all, as it shows that a PHP file is simply an HTML file with <?php ?> tags. These tags are parsed by PHP, evaluated, and anything that needed to get printed in the middle (or wherever) of the HTML file is printed right there. Then the resulting HTML file (including both the raw HTML and the results of your php tags) is sent to the client.
I am working with a software written in PHP that I have no control over. Some of the PHP files have hardcoded HTML tags in them that I'd like to suppress or exchange with my own code.
Fortunately there is a plugin-system that allows me to execute my own code at certain points, here's an example of the files I'm dealing with:
<!DOCTYPE html>
<html>
<?php
execute_plugin_code_1()
?>
<head>
<!-- some additional tags -->
</head>
<?php
execute_plugin_code_2()
?>
<body>
</body>
</html>
What I want to do here is generate my own <head> tag. I don't want to alter the file itself so I'd like to suppress the hardcoded tag and its children.
Is there any way to disable the output of hardcoded HTML between execute_plugin_code_1() and execute_plugin_code_2()?
To answer your question, no, you cannot "disable" the HTML tags. You would need to remove them from the file, or use an entirely different file. The structure of this file is pretty odd though. I'm not sure what the original developer was trying to allow by providing hooks that run just before, and just after the head of the HTML document. It would have made much more sense to provide a hook within the head and within the body.
Do you want to avoid jquery? If not, then
$(document).ready(function(){
$('head').remove();
)};
https://api.jquery.com/remove/
Then you'd need to also use jquery to insert your new head tag's html. I'd be very wary, though. Seems like it'd be pretty easy to break the page altogether.
My problem is that the webpage that I do is in complete PHP files. Everywhere there is tutorial how to generate, but there is no instructions what to do when it is a PHP file.
The main file is also index.php. When I insert the code that is generated in HTML.
Comes with a lot of errors, so I convert it to PHP.
it will start like
echo "..bla bla" "\n" ;
echo "<div property=\"gr:legalName\" content=\"G & Sziladi\"></div>\n";
which is good already, google recognize it, BUT this will show on the top of my page.How can i insert this markup into index.php without showing,but also google has to recognize it properly.
If you prefer to copy and paste HTML, the paste it into a PHP file but outside of the PHP tags:
<?php
/*
* May brand new PHP Website
*/
$title_prefix = '- Escaping the Tags';
?>
<html>
<head>
<title>Supersite Deluxe <?php echo $title_prefix; ?></title>
</head>
<body>
....
</body>
</html>
That is especially useful if you have to a lot of HTML, because you can just paste it in.
See Escaping from HTML
I have one question to ask you.
I have 2 PHP files first one is index.php and another one is body.php
index.php contain HTML template like
<html>
<head>
<title></title>
</head>
<body>
<? include('body.php') ?>
</body>
</html>
and body.php query data from database(such as name, nickname, age).
I need body.php to change tag or add more tag in index.php
How should i do in PHP command?
thanks
In your example, body.php can have any HTML output you need. The output of body.php will be included in your final output.
If you need to make the final output of index.php dependent on the body.php file, (for example to insert a title) you can load your content into variables, which can be outputted later.
<?
include ('body.php');
/* $title and $bodyHTML are set in the include file */
?>
<html>
<head>
<title><? echo $title; ?></title>
</head>
<body>
<? echo $bodyHTML;?>
</body>
</html>
You can use fopen() and fwrite() to modify the content of index.php from body.php (assuming that you have the write permissions, of course).
If you mean change the content while the user is viewing index.php and then change index.php, then that isn't possible without telling the user to "click here and view the new code!" (since at that point, you can no longer use headers to refresh the page).
PHP is not a dynamic content language like, for example, JavaScript.
You can't alter variables in part of the page that has already been output. You can use output buffering to capture the output to that point and then do string substitutions on it
<?php ob_start(); // start buffering output
?>
<html>
<head>
<title></title>
</head>
<body>
<?php
include('body.php');
// Get the contents of the buffer and then clear the buffer
$buffer = ob_get_clean();
// Replace your keyword with a variable loaded from body.php
$buffer = str_replace('%nickname%', $nickname, $buffer);
// output the altered head
echo $buffer;
// Stop buffering and output what we just echoed
ob_end_flush();
?>
</body>
</html>
There are a number of PHP template and theming engines out there that make
doing this kind of thing easier. Smarty is a fairly
popular one. Another one I like is Savant but I'm personally partial to the one I created called Enrober.
write db related stuff in body.php file and call those functions from index.php.
Loop those results and built through related tags and display.
Thats it.........
You could output the entire thing from a php object called domdocument, which allows dynamic creation of html documents.
That way, you could change the tags and their content as dynamically as you want.