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');
Related
In my own function, First i want to include one html file, Then if in that included file, we find another html file name, i want to merge content of first included file and second included file.
function inc($html){
$include_pattern = "\{\s*include\s*(\(|')\s*([a-zA-Z0-9-.\/-_]+)\s*(\)|')\s*\}";
preg_match_all("/$include_pattern/s",$html,$match);
if($len = count($match[0])){
$i=0;
while($i<$len){
$file = preg_replace("/$include_pattern/s","$2",$match[0][$i]);
ob_start();
include($file);
$output = ob_get_contents();
ob_end_clean();
//$output = $this->inc($output);
$html = str_replace($match[0][$i],$output,$html);
$i++;
}
return $html;
}
}
In this function, if you look at the commented part in the code, i was tried for get content of 2nd included html file. Imagine, if we include 1.html, if in this included file we have 2.html, how we can show content of 2.html and content of 1.html ?
If you look at image in below, its clear for understand current topic. ( i want to get result like "result box" in the image).
I fix issue in my function. There is changed function and work well...
function inc($html){
$include_pattern = "\{\s*include\s*(\(|')\s*([a-zA-Z0-9-.\/-_]+)\s*(\)|')\s*\}";
preg_match_all("/$include_pattern/s",$html,$match);
if($len = count($match[0])){
$i=0;
while($i<$len){
$file = preg_replace("/$include_pattern/s","$2",$match[0][$i]);
ob_start();
include($file);
$output = ob_get_contents();
ob_end_clean();
$html = str_replace($match[0][$i],$output,$html);
$i++;
}
return inc($html);
}else{
return $html;
}
}
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
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');
I know how to use file_get_contents and fopen etc, but when I do it to one of my own file, I get the literal string, meaning, the code is not preprocessed! How can i import text from a file without using require etc. because I want to store the value into a string
See examples #5 and #6 on the manual. Taken straight from there:
$string = get_include_contents('somefile.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;
}
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);
}