user3750757
user3750757

Reputation: 37

Contents of a file_get_content() to process PHP

I am need to use some PHP tags on a file(subfile) that is being called into a template using file_get_contents(Subfile).

I initially used require(subFile) instead of file_get_contents(). But it doesnt quite work as my content gets placed in the wrong spot. And also I can't change the template file to use require().

my template is something like this:

//TEMPLATE FILE.php
$html = file_get_contents(subFile); //I CAN NOT CHANGE  THIS FILE

Then my sub file, the one I can change.

//SUB FILE.php
<div>Hello world, today is <?php echo date($mydate);?> </div>

Because it is being called with file_get_contents(), it out puts

Hello world, today is <?php echo date($mydate);?>

Instead of:

Hello world, today is Thursday.

So, considering I can't change my TEMPLATE FILE.php where the file_get_content() is used. I was thinking if there is a way that I can wrap the content of SUB FILE.php so it would process the PHP before allowing it to be called by the TEMPLATE FILE.php.

Something like this I am after

//SUB FILE.php
<force Process the PHP ?>
<div>Hello world, today is <?php echo date($mydate);?> </div>
<Finishe Force Process ?>
<allow it to be called via file_get_content();

If I could change the template file I could use htmlspecialchars_decode(); but I can't.

Unfortunately I couldn't find a way to do that so I end up with this

ob_start();
$output = include (Dir/SubFile);
$output = ob_get_contents();
ob_end_clean();
$html = $output; 

//$html = file_get_contents(subFile); The way it was

Upvotes: 0

Views: 925

Answers (2)

Dinesh Rawat
Dinesh Rawat

Reputation: 1019

You cann't use <?php echo $a; ?> to echo the content of variable $a. Because you will get the plain html content from your subfile.

What you can do is to:-

Step1: my_date.php

<?php
$today = date('M-d-Y');
$subfile_html = file_get_contents(subFile);  //Path to my_date1.html or my_date1.php
echo $new_html_content = str_replace('{TODAYS_DATE}', $today, $subfile_html); // It will replace the occurance {TODAYS_DATE} with today's date

// OUTPUT:
// Hello world, today is June 20 2014
?>

Step2: my_date1.html or my_date1.php

<div> Hello world, today is {TODAYS_DATE} </div>

Upvotes: 0

colin chen
colin chen

Reputation: 11

The following is an example of the official manual

$homepage = file_get_contents('http://www.example.com');
echo $homepage;

try this:

file_get_contents('http://YourDomain/Yoursubfile'); 

Upvotes: 1

Related Questions