Henry Gunawan
Henry Gunawan

Reputation: 942

Put a file which have PHP syntax into a variable

I have searched for a while and I found out how to put a file into variable using this:

file_get_contents()

I used it for email purpose. But when I see the email, all the <php> tags and functions and syntax are written too as string.

How to solve this? Is there a way to put a file which has php syntax in it into a file just like codeigniter using $this->load->view("[filename]", true)?

--UPDATE--

To make it clearer, I'll add my code a bit. I put the code below in a file called preview.php and put it into a variable using file_get_contents() and then send it to an email.

<tr>
        <td><span class="section-title">1. </span></td>
        <td colspan="2"><span class="section-title">Course Chosen: <?php echo "Level ".$_POST['radioCourse'];?></span></td>
    </tr>
    <tr>
        <td>&nbsp;</td>
        <td colspan="2">
        <?php 
            if ($_POST['radioCourse'] == "1")
                echo $course[$_POST['dropLvl1Course']];
            else if ($_POST['radioCourse'] == "2")
                echo $course[$_POST['dropLvl2Course']];
            else if ($_POST['radioCourse'] == "3")
                echo $course[$_POST['dropLvl3Course']];
        ?>
        </td>
    </tr>
    <tr>
        <td><span class="section-title">2. </span></td>
        <td colspan="2"><span class="section-title">Preferred Start Date and Time</span></td>
    </tr>
    <tr>
        <td>&nbsp;</td>
        <td width="200px">1st Choice:</td>
        <td><?php echo $_POST['date1stChoice']." ".$_POST['textTime1stChoice'];?></td>
    </tr>
    <tr>
        <td>&nbsp;</td>
        <td>2nd Choice:</td>
        <td><?php echo $_POST['date2ndChoice']." ".$_POST['textTime2ndChoice'];?></td>
    </tr>

Here is the result in my email:

1.  Course Chosen:
    if ($_POST['radioCourse'] == "1")
    echo $course[$_POST['dropLvl1Course']];
    else if ($_POST['radioCourse'] == "2")
    echo $course[$_POST['dropLvl2Course']];
    else if ($_POST['radioCourse'] == "3")
    echo $course[$_POST['dropLvl3Course']];
?>
2.  Preferred Start Date and Time
    1st Choice:     
    2nd Choice:

Upvotes: 1

Views: 81

Answers (1)

472084
472084

Reputation: 17885

Try this:

ob_start();
include 'filename.php';
$filename = ob_get_clean();

It may not work if you already have output buffering related code in your script.

Upvotes: 1

Related Questions