mingfish_004
mingfish_004

Reputation: 1413

how to store output html to an var in php?

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?

?>

Upvotes: 1

Views: 64

Answers (5)

mingfish_004
mingfish_004

Reputation: 1413

finally, I got the answer.

<?php
    ob_start();
    require('a.php');
    $a=ob_get_clean();
    echo $a;    // get '1' but not 'hello world'


?>

Upvotes: 0

Chintan Gor
Chintan Gor

Reputation: 1072

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;

Upvotes: 0

Robbie Chiha
Robbie Chiha

Reputation: 409

You could also use shell_exec

<?php

    $out = shell_exec("php -s $File");   
?>

see: http://php.net/shell_exec

Upvotes: 1

Kovo
Kovo

Reputation: 1717

The result of the "require" is not what you want. Try:

ob_start();
require('a.php');
$a = ob_get_clean();
echo $a;

Upvotes: 2

Marty
Marty

Reputation: 39476

You're after ob_get_clean():

$a = ob_get_clean();

Upvotes: 3

Related Questions