This question already has answers here:
Return html response from PHP page in to variable
(2 answers)
Closed 9 years ago.
I have a php file myfile.php which is basically a lot of html and some php code within, for example:
<body>
Name: <?php echo $_GET['id'] ?>
<!-- and so on ... -->
</body>
I am using an open-source HTML to PDF converter written in PHP which requires as an input the html content to be converted:
PDFConverter::convertHTMLToPDFFIle($html_input, $filename_output_pdf);
How can I feed in the html generated from myfile.php?id=XX into $html_input?
You could use a little output buffering and include it.
<?php
ob_start();
include "myfile.php";
$content = ob_get_clean();
// $content now contains the processed code of myfile
At that point, the file contents has been processed by PHP because of the include and is in the $content variable. You can pass that to your convertHTMLToPDFFile() method.
PDFConverter::convertHTMLToPDFFile($content, $outFilename);
EDIT: OP edited question adding requirement that the included file needs to be able to accept variables.
When a file is included/required, it inherits the current scope. That means it has access to any variables, class definitions, functions, etc. that are defined. So, determine which variables are shared and set those values before including the file. For example:
<body>
Name: <?php echo $id; ?>
<!-- and so on ... ->
</body>
and then...
<?php
ob_start();
$id = <whatever the value should be>;
include "myfile.php";
$content = ob_get_clean();
If you have fopen url wrappers enabled on your host you can use file get contents
$html = file_get_contents("http://mysite.com/somefile.php?var=1&var2=2");
Related
This question already has answers here:
Get filename of file which ran PHP include
(7 answers)
Closed 5 years ago.
I want to get file name from URL in an included page for example in meta.php
I use basename(__FILE__) but I get 'meta.php'
how I can echo index.php in an included page??
<meta name="description" content="<?php
if(isset($FILEDATA_LANG['page_'.basename(__FILE__).'_keywords'])){ echo
$FILEDATA_LANG['page_'.basename(__FILE__).'_keywords']; }?>">
This has been asked earlier and you could've found your answer within seconds of searching, however, here's a proper response:
Set a variable in your index.php or a definition, such as:
define("THIS_PAGE", __FILE__); or $thisPage = __FILE__;
Then in your file you're including (after this variable), simply use:
<?php echo $thisPage; ?> or <?php echo THIS_PAGE; ?>.
(Keep in mind to check if the variable is set (isset($thisPage)) or (defined("THIS_PAGE")).
Reference: Get filename of file which ran PHP include
I'm trying by adding
.basename($_SERVER['SCRIPT_FILENAME']).
I Hope it would work when i publish this page
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.
This question already has answers here:
Include another HTML file in a HTML file
(41 answers)
Closed 8 years ago.
for example my website is "myweb.com". how can i do opration like following.
include(http://myweb.com/file);
this is an internal URL.
for example i want to include
http://myweb.com/process.php?action=update
this is not a file
"?action=update"
thus how can i do this operation?
To include pages with PHP, simply put the following code in a.html, where you would like the code to appear:
<?php
include "b.html";
?>
You can't include an HTML document into another HTML document.
You can do that using PHP but the file that includes the other must be a PHP document. In your example a includes b therefore a is a.php while b can stay b.html.
Then inside a.php you can write:
<?php echo file_get_contents('b.html'); ?>
and the content of b.html will be included into a.php.
This question already has answers here:
How to create a template in HTML?
(3 answers)
Closed 9 years ago.
I looked around, but couldn't find a satisfying answer.
Problem:
I have a menu bar that appears on the top of the page. I want it to show across all of the pages on the website. So how would someone do that without copying the same code each time. Would someone use html, php, css, or javascript/jQuery to accomplish this?
Note: I want to have a separate html file to access the information from.
From what I have seen, this is typically done with php using a template file.
The template file may have HTML code in it that you want to display on every page, as well as placeholders for content that is page specific. e.g: template.php
<html>
<head>
<title><?php print $title; ?></title>
</head>
<body>
<nav>Test</nav>
<?php print $content; ?>
</body>
</html>
In this case, as long as $title and $content variables are set, you can then do a include 'template.php'; to output this HTML code in other php files.
Read more about php's include.
It seems that you will need to use include, although an explaination on how to use it (or at least an example can be found here: https://www.youtube.com/watch?v=XmoF-6vshSI
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.