how to store output html to an var in php ?
how to set $a = require(xx.php)?
the xx.php outputs html , i want to store this html to a var , how to do?
please see my codes bellow:
a.php codes:
<?php
// a.php
echo 'hello world';
?>
b.php
<pre>
<?php
ob_start();
$a=require('a.php');
ob_clean();
echo $a; // get '1' but not 'hello world'
// can not post , why?
// can not post , why?
// can not post , why?
// can not post , why?
// can not post , why?
// can not post , why?
?>
You're after ob_get_clean():
$a = ob_get_clean();
The result of the "require" is not what you want. Try:
ob_start();
require('a.php');
$a = ob_get_clean();
echo $a;
You could also use shell_exec
<?php
$out = shell_exec("php -s $File");
?>
see: http://php.net/shell_exec
Hi according to your case you want output of any PHP page which you want to use in your current page in that case you need to use CURL in PHP
$ch = curl_init('a.php');
$htmlResponse = curl_exec($ch);
curl_close($ch);
echo $htmlResponse ;
die;
finally, I got the answer.
<?php
ob_start();
require('a.php');
$a=ob_get_clean();
echo $a; // get '1' but not 'hello world'
?>
Related
sorry for my last question where i try put some live code with ob_start buffer content is not helping me to solve my problem because buffer content just collects output text, it doesn't execute any code. thanks #akrys for your advices
what i want is to put code into while looping like this
$sql = $conn->query("SELECT * FROM `users`");
$var = $row['full_name'];
include('test.php');
after i call test.php contain while code like:
while($row = $sql->fetch_array()) {
echo $var;
}
everything is work if i replace $var with $row['full_name'];
but i get the name of row field from some script on index.php so i should access that file first then i call portable file contain query to fetch_array on test.php
how to make it work when i put it back with $var contain variable field name
thank you very much for your attention guys
you should to include before your code
page
test.php
<?php
$someVariable = 'hello'; // the variable only can access in here
?>
<?php
include('test.php');
ob_start();
echo "some text with call variable $someVariable";
echo "other stuff";
$tdcol1_val = ob_get_contents(); ob_clean();
echo $tdcol1_val; //
?>
of course you can use define too
page test.php
<?php
define( "SOMEVARIABLE", hello );
?>
<?php
include('test.php');
ob_start();
echo "some text with call variable ".SOMEVARIABLE;
echo "other stuff";
$tdcol1_val = ob_get_contents(); ob_clean();
echo $tdcol1_val; //
?>
you can use:
define("CONSTANT", "Hello world.");
echo CONSTANT; // outputs "Hello world."
for more help, use the link below:
enter link description here
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.
Hello i have a php file called a.php that includes a specific value
example : $a = 0;
i just want to print this value (a) in a html file called b.html is that possible ? thanks
Sure, you have to open php tag, include php file, echo variable and close php tag
For example:
<p><?php include('a.php'); echo $a; ?></p>
This will work..
<?php
ob_start();
$a = 1;
echo $a; // echo $a value
file_put_contents('b.html', ob_get_contents()); // save the output to b.html
?>
However, if you already have other content in the existing page all of them will be there in the html file. Therefore you can POST the variable $a to a separate php file as follows..
<?php
ob_start();
$a = $_POST["aVar"];
echo $a;
file_put_contents('b.html', ob_get_contents());
?>
I want to call require_once("test.php") but not display result and save it into variable like this:
$test = require_once('test.php');
//some operations like $test = preg_replace(…);
echo $test;
Solution:
test.php
<?php
$var = '/img/hello.jpg';
$res = <<<test
<style type="text/css">
body{background:url($var)#fff !important;}
</style>
test;
return $res;
?>
main.php
<?php
$test = require_once('test.php');
echo $test;
?>
Is it possible?
Yes, but you need to do an explicit return in the required file:
//test.php
<? $result = "Hello, world!";
return $result;
?>
//index.php
$test = require_once('test.php'); // Will contain "Hello, world!"
This is rarely useful - check Konrad's output buffer based answer, or adam's file_get_contents one - they are probably better suited to what you want.
“The result” presumably is a string output?
In that case you can use ob_start to buffer said output:
ob_start();
require_once('test.php');
$test = ob_get_contents();
EDIT From the edited question it looks rather like you want to have a function inside the included file. In any case, this would probably be the (much!) cleaner solution:
<?php // test.php:
function some_function() {
// Do something.
return 'some result';
}
?>
<?php // Main file:
require_once('test.php');
$result = test_function(); // Calls the function defined in test.php.
…
?>
file_get_contents will get the content of the file. If it's on the same server and referenced by path (rather than url), this will get the content of test.php. If it's remote or referenced by url, it will get the output of the script.
I tried:
$test = include 'test.php';
But that just included the file normally
You'll want to look at the output buffering functions.
//get anything that's in the output buffer, and empty the buffer
$oldContent = ob_get_clean();
//start buffering again
ob_start();
//include file, capturing output into the output buffer
include "test.php";
//get current output buffer (output from test.php)
$myContent = ob_get_clean();
//start output buffering again.
ob_start();
//put the old contents of the output buffer back
echo $oldContent;
EDIT:
As Jeremy points out, output buffers stack. So you could theoretically just do something like:
<?PHP
function return_output($file){
ob_start();
include $file;
return ob_get_clean();
}
$content = return_output('some/file.php');
This should be equivalent to my more verbose original solution.
But I haven't bothered to test this one.
Try something like:
ob_start();
include('test.php');
$content = ob_get_clean();
Try file_get_contents().
This function is similar to file(), except that file_get_contents() returns the file in a string.
Solution #1: Make use of include (works like a function): [My best solution]
File index.php:
<?php
$bar = 'BAR';
$php_file = include 'included.php';
print $php_file;
?>
File included.php:
<?php
$foo = 'FOO';
return $foo.' '.$bar;
?>
<p>test HTML</p>
This will output FOO BAR, but
Note: Works like a function, so RETURN passes contents back to variable (<p>test HTML</p> will be lost in the above)
Solution #2: op_buffer():
File index.php:
<?php
$bar = 'BAR';
ob_start();
include 'included.php';
$test_file = ob_get_clean(); //note on ob_get_contents below
print $test_file;
?>
File included.php:
<?php
$foo = 'FOO';
print $foo.' '.$bar;
?>
<p>test HTML</p>
If you use ob_get_contents() it will output FOO BAR<p>test HTML</p> TWICE, make sure you use ob_get_clean()
Solution #3: file_get_contents():
File index.php:
<?php
$bar = 'BAR';
$test_file = eval(file_get_contents('included.php'));
print $test_file;
?>
File included.php:
$foo = 'FOO';
print $foo.' '.$bar;
This will output FOO BAR, but Note: Include.php should not have <?php opening and closing tags as you are running it through eval()
The other answers, for reasons unknown to me, don't quite reach the correct solution.
I suggest using the buffer, but you have to get the contents and then clean the buffer before the end of the page, otherwise it is outputted. Should you wish to use the output from the included file, you should use op_get_contents(), which will return a string of the contents of the buffer.
You also don't need to loop over the includes as each will just add to the buffer (unless you clean it first).
You therefore could use the following;
ob_start();
include_once('test.php');
include_once('test2.php');
$contents = ob_get_contents();
ob_end_clean();
Hope this helps.
You can use the function file_get_contents.