2012-12-01 42 views
0

PHP中是否可以將HTML代碼的輸出抓取到變量中?基本上,我在尋找此代碼的縮寫:將html輸出打包到變量中

<?php ob_start(); ?> 
<div class="headSection"> 
    <h1><?=$headline?></h1> 
    <p><?=$bottomline?></p> 
</div> 
<?php $contents = ob_get_contents(); ob_end_clean(); ?> 

我想要的HTML代碼保存在$contents變量。

+0

沒有我的回答爲你工作? – Alex

+0

@ w0rldart對不起,對不起:) –

回答

3

使用PHP的EOF字符串

$str = <<<EOF 
<div class="headSection"> 
    <h1>$headline</h1> 
    <p>$bottomline</p> 
</div> 
EOF; 
    echo $str; 

演示http://codepad.org/9OeUiJNJ

+0

如果你想在PHP 5.3中模擬不會分析變量的單引號字符串,你可以使用'<<<'EOF'',稱爲nowdoc – pocesar