开发者

how to get the result from a php file into a string

example i have a file template.php with:

<table>
    <tr>
        <td><?php echo $data['nombre'] ?></td>
    </tr>
    <?php foreach($data['values] as $value): ?>
   开发者_运维知识库 <tr>
        <td><?php echo $value ?> </td>         
    </tr>
    <?php endforeach; ?>
</table>

and i need the result into a string $result = get_content_process('template.php',$data); for use in other process.

echo $result;
<table>
    <tr>
        <td>Juan</td>
    </tr>
    <tr>
        <td>Male</td>         
    </tr>
    <tr>
        <td>Brown</td>         
    </tr>
</table>


<?php
ob_start();
include 'template.php';
$result = ob_get_clean()
?>

this should do, the $result is the string you need


To make sure you don't flush early, turn implicit flush off. This function should do the trick:

function get_content_process($template, $data) {
   ob_implicit_flush(false);
   include($template);
   $contents = ob_get_contents();
   ob_clean();
   ob_implicit_flush(true);
   return $contents;
}


You can use ob_start() to do something like this

<?php
    ob_start( );
    $GLOBALS['data'] = ...;
    include("template.php");
    $result = ob_get_clean();
    echo $result;
?>


simple and fast solution:

$result = file_get_contents($view); // $view == the address of the file(ie 'some_folder/some_file.php')
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜