9

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"
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
ppp
  • 2,035
  • 9
  • 32
  • 52

4 Answers4

36
ob_start();
include 'test.php';
$string = ob_get_clean();

I think is what you want. See output buffering.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
DaveRandom
  • 87,921
  • 11
  • 154
  • 174
  • 3
    That technique is called output buffering for anyone curious. – Alex Turpin Apr 13 '12 at 15:59
  • @Xeon06 Just added a manual link while you were writing the comment! Cheers for the prod though :-) – DaveRandom Apr 13 '12 at 16:00
  • 1
    @nickb [No it doesn't](http://php.net/manual/en/function.ob-get-clean.php), `ob_end_clean()` returns `bool`. `ob_get_clean()` is a shortcut to calling `$str = ob_get_contents(); ob_end_clean();` – DaveRandom Apr 13 '12 at 16:02
6
ob_start();
include($file);
$contents = ob_get_contents(); // data is now in here
ob_end_clean();
fire
  • 21,383
  • 17
  • 79
  • 114
4

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.

Prestaul
  • 83,552
  • 10
  • 84
  • 84
  • Problem with wrapping it in a function is variable scope... – DaveRandom Apr 13 '12 at 15:58
  • this is what I was doing wrong. I came across this method in another answer, but I got an undeclared variable error since the variables were declared outside the scope of the function. – ppp Apr 13 '12 at 16:00
  • @AnPel: Well then you have to fix your variables. The technique stays the same though. – Felix Kling Apr 13 '12 at 16:02
  • @AnPel A dirty workaround is to `extract($GLOBALS);` in the function before you include the file, but this is not a one-solution-fits-all, since it will not work if you call `includ2string()` from inside another function. – DaveRandom Apr 13 '12 at 16:05
  • Good call. I was actually about to add a note about extract, but I was not going to extract $GLOBALS because it just seems so wrong. :) – Prestaul Apr 13 '12 at 16:08
  • Indeed, I have never done it, it seems like one of those "may bring about the end of the universe" things, since it contains a reference to itself... – DaveRandom Apr 13 '12 at 16:09
  • lol. yeah... I actually have done it and the world did not turn itself inside-out, but it felt very wrong and we didn't leave it long (just for prototyping a solution). – Prestaul Apr 13 '12 at 16:12
0

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

David Bélanger
  • 7,400
  • 4
  • 37
  • 55
  • This gets the contents but won't execute the PHP in the include and will return the raw PHP code. – MrCode Apr 13 '12 at 16:05
  • True, I didn't saw the keyword execute in the OP post ... I may had misunderstood the question. – David Bélanger Apr 13 '12 at 16:07
  • @MrCode I suppose you could `eval()` (evil) it, but it still suffers the variable scoping problem that you get from doing it in a function and is probably more expensive than just `include()`ing it in the first place – DaveRandom Apr 13 '12 at 16:08