Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

file_get_contents and php code

Tags:

include

php

Hello is there a way to take the content of a php file while processing the php code in it? I need to include a code into another page but I need to print the content it in a specific position.

If I use include the code will be printed before the html tag of course cause it's processed before the page, but if I use file_get_contents the content is taken in the page but if I have some php tags I'll get those too as plain text.

Thank you.

EDIT: sorry guys seems like I was drunk while I was writing. I corrected.

I have an engine that processes the page contents, puts them into a variable and then print them in a specific position within the html page. In the engine I need to "embed" the code of other "static" pages that could have some php tags. If I use file_get_contents I will get the content as plain text (with php tags not parsed) if I use include it just won't work cause it's not the function for it. So I what I need is to embed the PROCESSED code into the engine (as ready-to-be-printed HTML).

like image 334
Sandro Antonucci Avatar asked Nov 22 '10 00:11

Sandro Antonucci


People also ask

What does file_get_contents do in PHP?

The file_get_contents() reads a file into a string. This function is the preferred way to read the contents of a file into a string. It will use memory mapping techniques, if this is supported by the server, to enhance performance.

What is the difference between the file () and file_get_contents () functions?

file — Reads entire file contents into an array of lines. file_get_contents — Reads entire file contents into a string.

How do I echo content in PHP?

The file_get_contents function takes the name of the php file and reads the contents of the text file and displays it on the console. get the contents, and echo it out.


1 Answers

You can use an output buffer to include a PHP file but save the resulting output for later use instead of printing it immediately to the browser.

ob_start();
include('path/to/file.php');
$output = ob_get_contents();
ob_end_clean();

// now do something with $output
like image 173
ceejayoz Avatar answered Oct 08 '22 00:10

ceejayoz