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 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,