0

I want to use file_get_contents() on an html file with 3 $vars written inside it and have those vars get the data assigned to them through $_POST.

example:

-html file-
    <html>
    .
    .
    <table>
    <tr>
    <td>first name</td><td>last name</td><td>id</td>
    </tr>
    <tr>
    <td>$fname</td><td>$lname</td><td>$id</td>
    </tr>
    </table>
    </html>

-php file-
    <?php
    .
    .
    $fname = $_POST('fname');
    $lname = $_POST('lname');
    $id = $_POST('id');
    $page = file_get_contents("test.html");
    echo $page;
    ?>

what I did for now was to set a comment "<!--split-->" where the vars go and then I explode() the file_get_contents("test.html"), attach the vars to the end of it and implode() the $page. but it seems kinda intensive for such a small task and I hoped for a better solution. I hope I have been clear enough with my question. if not please ask and I'll try to clarify more if I can.

4

2 に答える 2

2

This is a solution which would require control of the test.html file:

1: Rename test.html to test.php

2: Edit test.php so it looks like this (notice I added the echo keyword, surrounded by PHP opening and closing tags):

<?php
//View (PHP file)
?>
<html>

    <table>
        <tr>
            <td>first name</td><td>last name</td><td>id</td>
        </tr>
        <tr>
            <td><?php echo $fname; ?></td>
            <td><?php echo $lname; ?></td>
            <td><?php echo $id; ?></td>
        </tr>
    </table>

</html>

3: Now, in your main PHP file, just include the PHP template file:

$fname = $_POST('fname');
$lname = $_POST('lname');
$id = $_POST('id');
include 'test.php';
于 2012-09-11T01:59:44.753 に答える
1

What about:

-html file-
    <html>
    .
    .
    <table>
    <tr>
    <td>first name</td><td>last name</td><td>id</td>
    </tr>
    <tr>
    <td>%s</td><td>%s</td><td>%s</td>
    </tr>
    </table>
    </html>

-php file-
    <?php
    .
    .
    $fname = $_POST('fname');
    $lname = $_POST('lname');
    $id = $_POST('id');
    $page = sprintf(file_get_contents("test.html"),$fname,$lname,$id);
    echo $page;
    ?>
于 2012-09-11T01:49:11.420 に答える