duanfuxing2417 2013-06-20 13:55
浏览 49
已采纳

混合HTML和PHP代码中的字符串操作

The code below is a sample code of the top oh my php web page. There are php variables being outputted in specific places.

I'd to implement an HTML to PDF converter, but it requires me to put all of my code into a single variable that the PDF converter will use in its class. How can I put my existing into into a single variable say: $html without having to open up all my PHP variables, escpaing everything and concatenating the whole place? I was thinking of using heredocsyntax but it doesn't like the <?php ?> and I'm sort of confused as I've never used it in the past. Any ideas on how to achieve this?

Ideally, this is what I'd like to do:

$html = <<<EOD
<div id="topHeaderView"><?php echo configuration::getValue(6); ?></div>
  <table>
   <tr>
     <td><?php echo $lang["FAI_R"]["PRT"]["TITLE"]["HEADER"]; ?></td>
   </tr>
EOD;

The above doesn't capture any values outputted by $lang["FAI_R"]["PRT"]["TITLE"]["HEADER"] or by configuration::getValue(6).

Insead of:

$html = "";
$html .= "<div id=\"topHeaderView\">".configuration::getValue(6)."</div>";
$html .= "<table>";
$html .= "<tr>";
$html .= "<td>".$lang["FAI_R"]["PRT"]["TITLE"]["HEADER"]."</td>";
$html .= "</tr>";

This is something I want to avoid...

  • 写回答

4条回答 默认 最新

  • dongli8862 2013-06-20 14:31
    关注

    This is a good use of output buffering

    ob_start();
    ?><div id="topHeaderView"><?php echo configuration::getValue(6); ?></div>
    <table>
        <tr>
            <td><?php echo $lang["FAI_R"]["PRT"]["TITLE"]["HEADER"]; ?></td>
        </tr>
    <?php
    $html = ob_get_clean();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题