PHP require return echo? - php

Is it possible to require a php file and get all the things that's echo'ed to be returned and stored into a variable?
Example:
//file1.php
// let's say $somevar = "hello world"
<p><?php echo $somevar; ?></p>
//file2.php
$file1 = getEchoed("file1.php");
// I know getEchoed don't exist, but i'm unsure how to do it.

Use output buffering:
ob_start();
require('somefile.php');
$data = ob_get_clean();

Output buffering can do what you need.
ob_start();
require("file1.php");
$file1 = ob_get_contents();
ob_clean();

ob_start();
include('file1.php');
$contents = ob_get_clean();
The output from file1.php is now stored in the variable $contents.

Output buffering:
<?php
ob_start();
require 'file1.php';
$var_buffer = ob_get_contents();
ob_end_clean();
echo $var_buffer;
?>

Related

How to use ob_get_contents with a function with arguments

I have a function like this:
function show_aval($place) {
//some function
}
I want to translate this into a variable so I do this:
ob_start();
show_aval(london);
$avalrooms = ob_get_contents();
ob_clean();
This will be fine if the argument is always "london" which is not. It may change to York or Manchester, etc. My question is what can I do in order to indicate the argument through the variable like this: $avalrooms["london"], $avalrooms["york"], $avalrooms["liverpool"], etc. without having to declare each variable one by one.
EDIT:
the code below:
ob_start();
show_aval(london);
$show_aval = ob_get_contents();
ob_clean();
ob_start();
show_aval(york);
$show_aval2 = ob_get_contents();
ob_clean();
ob_start();
show_aval(liverpool);
$show_aval3 = ob_get_contents();
ob_clean();
will work but its just too much code. I tried with foreach but that seems to get ob_get_contents nullified.
Put the cities in an array, loop over the array, and use them as the function argument and array index.
$avalrooms = [];
$cities = ["london", "york", "liverpool"];
foreach ($cities as $city) {
ob_start();
show_aval($city);
$avalrooms[$city] = ob_get_contents();
ob_clean();
}

Adding included file contents to a variable

I am trying to add a the contents of an include file to a variable.
For example I have a variable called $form:
$form = 'Hello world';
$form .= include('sidebar.inc.php');
Is there another way to do this? Because the method noted above is causing me some problems...
Instead of using include(), you can use file_get_contents() to store it in a variable:
$form = file_get_contents('sidebar.inc.php');
That won't parse the PHP before storing it in your variable so another option is a solution posted here from the include manual:
$string = get_include_contents('sidebar.inc.php');
function get_include_contents($filename) {
if (is_file($filename)) {
ob_start();
include $filename;
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}
return false;
}
ob_start();
include('sidebar.inc.php');
$var = ob_get_flush();
file_get_contents('sidebar.inc.php') does not parse php files. (your IF LOOP ELSE stuff)
$form = file_get_contents('sidebar.inc.php');

How to get include contents as a string? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Execute a PHP file, and return the result as a string
PHP capture print/require output in variable
I am trying to get the contents of an include to a string. Is that possible?
For example, if I have a test.php file:
echo 'a is equal to '.$a;
I need a function, say include_to_string to include the test.php and return what would be output by in in a string.
Something like:
$a = 4;
$string = include_to_string(test.php); // $string = "a is equal to 4"
ob_start();
include 'test.php';
$string = ob_get_clean();
I think is what you want. See output buffering.
ob_start();
include($file);
$contents = ob_get_contents(); // data is now in here
ob_end_clean();
You can do this with output buffering:
function include2string($file) {
ob_start();
include($file);
return ob_get_clean();
}
#DaveRandom points out (correctly) that the issue with wrapping this in a function is that your script ($file) will not have access to variable defined globally. That might not be an issue for many scripts included dynamically, but if it is an issue for you then this technique can be used (as others have shown) outside of a function wrapper.
** Importing variables
One thing you can do is to add a set of data you would like to expose to your script as variables. Think of it like passing data to a template.
function include2string($file, array $vars = array()) {
extract($vars);
ob_start();
include($file);
return ob_get_clean();
}
You would call it this way:
include2string('foo.php', array('key' => 'value', 'varibleName' => $variableName));
and now $key and $variableName would be visible inside your foo.php file.
You could also provide a list of global variables to "import" for your script if that seems clearer to you.
function include2string($file, array $import = array()) {
extract(array_intersect_key($GLOBALS, array_fill_keys($import, 1)));
ob_start();
include($file);
return ob_get_clean();
}
And you would call it, providing a list of the globals you would like exposed to the script:
$foo='bar';
$boo='far';
include2string('foo.php', array('foo'));
foo.php should be able to see foo, but not boo.
You could also use this below but I recommend the above answer.
// How 1th
$File = 'filepath';
$Content = file_get_contents($File);
echo $Content;
// How 2th
function FileGetContents($File){
if(!file_exists($File)){
return null;
}
$Content = file_get_contents($File);
return $Content;
}
$FileContent = FileGetContents('filepath');
echo $FileContent;
Function in PHP manual : file_get_contents

PHP Output to variable

File-1:
<?php
<div>
<p>
something...
</p>
</div>
?>
File-2:
<?php
$file1= What should I write to get output of File1?
?>
I need to get html output to the $file1 variable;
Please help.
I think this will work for you:
function read($file) {
ob_start();
include($file);
$content = ob_get_contents();
ob_end_clean();
return $content;
}
Use it like this:
$file = 'path/to/your/file.php';
$content = read($file);
To pass variables you can modify function above like this:
function read($file, $vars) {
ob_start();
extract($vars, EXTR_SKIP);
include($file);
$content = ob_get_contents();
ob_end_clean();
return $content;
}
$file = 'path/to/your/file.php';
$vars = array(
'var1' => 'value',
);
$content = read($file, $vars);
You can access variables in included file like this: print $var1;
you can use
http://www.php.net/manual/en/function.ob-start.php
to start output buffering, then include your file and use
http://www.php.net/manual/en/function.ob-get-contents.php
to read what was the output and finish with
http://www.php.net/manual/en/function.ob-end-clean.php
to end the buffering.
Maybe is this you are looking for:
http://www.tizag.com/phpT/fileread.php
Visit the section:
PHP - File Read: fread Function
You could either set the variable in File-1 and include it:
include() / include_once() / require() / require_once()
Or you could print the contents of File-1 and use output buffering:
http://www.php.net/manual/en/function.flush.php
Use this code:
$content = file_get_contents('somefile.txt');

PHP - print content from file after manipulation

I'm struggling trying to read a php file inside a php and do some manipulation..after that have the content as a string, but when I try to output that with echo or print all the php tags are literally included on the file.
so here is my code:
function compilePage($page,$path){
$contents = array();
$menu = getMenuFor($page);
$file = file_get_contents($path);
array_push($contents,$menu);
array_push($contents,$file);
return implode("\n",$contents);
}
and this will return a string like
<div id="content>
<h2>Here is my title</h2>
<p><? echo "my body text"; ?></p>
</div>
but this will print exactly the content above not compiling the php on it.
So, how can I render this "compilePage" making sure it returns a compiled php result and not just a plain text?
Thanks in advance
function compilePage($page, $path) {
$contents = getMenuFor($page);
ob_start();
include $path;
$contents .= "\n".ob_get_clean();
return $contents;
}
To evaluate PHP code in a string you use the eval function, but this comes highly unadvised. If you have a file containing PHP code, you can evaluate it with include, include_once, require, or require_once depending on your need. To capture the output of an included file - or required, or whichever method - you need to enable output buffering.
You can use output buffering for this, and include the file normally:
function compilePage($page,$path){
$contents = array();
$menu = getMenuFor($page);
ob_start();
include $path;
$file = ob_get_contents();
ob_end_clean();
array_push($contents,$menu);
array_push($contents,$file);
return implode("\n",$contents);
}
The include() call will include the PHP file normally, and <?php blocks will be parsed and executed. Any output will be captured by the buffer created with ob_start(), and you can get it later with the other ob_* functions.
You need to use include() so it will execute. You can couple this with output buffering to get the return in a string.
function compilePage($page,$path){
$contents = array();
$menu = getMenuFor($page);
//output buffer
ob_start();
include($path);
$file = ob_get_contents();
ob_end_clean();
array_push($contents,$menu);
array_push($contents,$file);
return implode("\n",$contents);
}

Categories