How to capture HTML for a PHP variable - php

I have dozens of templates elaborated by a web designer. They are primarily HTML, with a few PHP tags and data. The problem is that I need to re-use those templates (tpl.php) to send email, and the email body (through PHPMailer) is a variable. I have succeeded to fill a variable with the HTML output in the code example (adapted). My question is: should I redo all the templates or is this a valid approach?
<?php
print "This will print just the 'hello world' output. I don't need to print the function as it has no return value<br />";
hola_mundo(); // I directly call the function and it outputs HTML to the screen
print "<br />";
// Now the assignment to the variable.
ob_start(); // I silence the output to screen
hola_mundo();
$string = ob_get_contents(); // I capture the buffer
ob_end_clean(); // I restore the output to screen
print "Now I print the string variable to demonstrate it has captured the HTML ";
print $string;
function hola_mundo(){
?><font color="red"<b>HOLA MUNDO CRUEL</b></font><?php
} // function
?>
The more logical approach would be to have this function (which I do not have and should redo for dozens of templates):
<?php
$string = hola_mundo();
print $string;
function hola_mundo(){
$string = '<font color="red"<b>HOLA MUNDO CRUEL</b></font>';
return $string
} // function
?>

Do you mean this?
function getTemplate($file) {
ob_start();
include $file;
return ob_get_clean();
}
// Example usage:
$string = getTemplate('templates/tpl.php');
tpl.php will be executed as a PHP file.
You can even pass variables to the template:
function getTemplate($file, $variables=array()) {
extract($variables);
ob_start();
include $file;
return ob_get_clean();
}
// Example usage:
$string = getTemplate('templates/tpl.php', array('message' => 'Hello world!'));
This will extract 'Hello world' into the function scope, making it available as the $message variable to the template.

Related

Is it possible to have access to all the strings written before, on a page, with PHP?

Does PHP give access to all the strings that have been outputted to a page?
<html>
<body>
Link
<?php
echo 'hello world';
echo 'something else';
$s = get_all_output(); // does this exist ?
Of course I could replace every instance of echo 'some text'; by an initial $s = ''; and then $s .= 'some text';.
But without this trick, how to get the current page as a string?
You could make use of the output buffering of PHP (cf. https://www.php.net/manual/en/function.ob-get-contents.php).
<?php
ob_start(); // turn on buffering
echo "Hello ";
$out = ob_get_contents();
ob_end_clean(); // end buffering and clear buffer without "displaying it".
// process $out which contains "Hello "
echo "$out";
You must insert:
ob_start();
at the beginig of your php code and then
$s = ob_get_contents();
at the point where you want to get the content of the page.

Include a php file, but return output as a string instead of printing

I want to include a file, but instead of printing output I want to get it as string.
For example, I want include a file:
<?php echo "Hello"; ?> world!
But instead of printing Hello world! while including the file I want to get it as a string.
I want to filter some elements from the file, but not from whole php file, but just from the html output.
Is it possible to do something like this?
You can use php buffers like this:
<?php
ob_start();
include('other.php');
$script = ob_get_contents(); // it will hold the output of other.php
ob_end_clean();
EDIT: You can abstract this into a function:
function inlcude2string($file) {
ob_start();
include($file);
$output = ob_get_contents(); // it will hold the output of other.php
ob_end_clean();
return $output;
}
$str = inlcude2string('other.php');

How do I populate a variable using a function?

I have the code below on a page basically what I'm trying to do is fill $content variable using the function pagecontent. Anything inside pagecontent function should be added to the $content variable and then my theme system will take that $content and put it in theme. From the answers below it seems you guys think I want the html and php inside the actual function I don't.
This function below is for pagecontent and is what I'm currently trying to use to populate $content.
function pagecontent()
{
return $pagecontent;
}
<?php
//starts the pagecontent and anything inside should be inside the variable is what I want
$content = pagecontent() {
?>
I want anything is this area whether it be PHP or HTML added to $content using pagecontent() function above.
<?php
}///this ends pagecontent
echo functional($content, 'Home');
?>
I think you're looking for output buffering.
<?
// Start output buffering
ob_start();
?> Do all your text here
<? echo 'Or even PHP output ?>
And some more, including <b>HTML</b>
<?
// Get the buffered content into your variable
$content = ob_get_contents();
// Clear the buffer.
ob_get_clean();
// Feed $content to whatever template engine.
echo functional($content, 'Home');
As you are obviously a beginner here's a much simplified, working version to get you started.
function pageContent()
{
$html = '<h1>Added from pageContent function</h1>';
$html .= '<p>Funky eh?</p>';
return $html;
}
$content = pageContent();
echo $content;
The rest of the code you post is superfluous to your problem. Get the bare minimum working first then move on from there.
Way 1:
function page_content(){
ob_start(); ?>
<h1>Hello World!</h1>
<?php
$buffer = ob_get_contents();
ob_end_clean();
return $buffer;
}
$content .= page_content();
Way 2:
function page_content( & $content ){
ob_start(); ?>
<h1>Hello World!</h1>
<?php
$buffer = ob_get_contents();
ob_end_clean();
$content .= $buffer;
}
$content = '';
page_content( $content );
Way 3:
function echo_page_content( $name = 'John Doe' ){
return <<<END
<h1>Hello $name!</h1>
END;
}
echo_page_content( );

php evaluate code before getting file content

I have a file B590.php which is having a lot of html code and some php code (eg logged in username, details of user).
I tried using $html = file_get_content("B590.php");
But then $html will have the content of B90.php as plain text(with php code).
Is there any method where I can get the content of the file after it has been evaluated?
There seems to be many related questions like this one and this one but none seems to have any definite answer.
You can use include() to execute the PHP file and output buffering to capture its output:
ob_start();
include('B590.php');
$content = ob_get_clean();
function get_include_contents($filename){
if(is_file($filename)){
ob_start();
include $filename;
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}
return false;
}
$html = get_include_contents("/playbooks/html_pdf/B580.php");
This answer was originally posted on Stackoverflow
If you use include or require the file contents will behave as though the current executing file contained the code of that B590.php file, too. If what you want is the "result" (ie output) of that file, you could do this:
ob_start();
include('B590.php');
$html = ob_get_clean();
Example:
B590.php
<div><?php echo 'Foobar'; ?></div>
current.php
$stuff = 'do stuff here';
echo $stuff;
include('B590.php');
will output:
do stuff here
<div>Foobar</div>
Whereas, if current.php looks like this:
$stuff = 'do stuff here';
echo $stuff;
ob_start();
include('B590.php');
$html = ob_get_clean();
echo 'Some more';
echo $html;
The output will be:
do stuff here
Some more
<div>Foobar</div>
To store evaluated result into some variable, try this:
ob_start();
include("B590.php");
$html = ob_get_clean();
$filename = 'B590.php';
$content = '';
if (php_check_syntax($filename)) {
ob_start();
include($filename);
$content = ob_get_clean();
ob_end_clean();
}
echo $content;

Get code between php tags with regular expression

I'm trying to get php code out of a HTML template file to execute it and place the result back.
What regular expression code can I use? And is there a method that also return the position of the found first tag?
<p>some html</p>
<?php $some = "php code"; ?>
<p>some <em>more</em> html</p>
<?php $some = "more php code"; ?>
I want the php code filtered from the html.
preg_match("/<\?.*?\?>/m",$output,$matches)
or
preg_match("/<\?.*?\?>/s",$output,$matches)
Should match all lines.
Instead of "getting the PHP out and putting it back" you should pass the variables to the template file.
Something like this:
<?php
function loadTemplate($path,$data=array()){
if (file_exists($path) === false){
throw new Exception('Template not found:'.$path);
return false;
}
extract($data);
ob_start();
require($path);
$return = ob_get_contents();
ob_end_clean();
return $return;
}
$vars = array('var1'=>$value,
'var2'=>$somevalue,
'var3'=>$someothervalue,
'var4'=>$blab);
$template = loadTemplate('path/to/thefile.php',$vars);
?>
Then access the $vars array values from within thefile.php like
echo $var1
echo $var2
ect
Hope it helps

Categories