【问题标题】:file_get_contents page does not support phpfile_get_contents 页面不支持 php
【发布时间】:2011-06-17 01:51:08
【问题描述】:

我正在使用我的 php 新网站。这里我想用php文件写代码写一个html页面。在此我使用了 file_get_contents。例如。见下文

$page_old.="<table width='1051' border='0' cellspacing='0' cellpadding='0'>";
$page_old.="<tr><td height='50' class='tittle'>Test</td></tr>";
$page_old.="<tr><td class='content'>My content Body</td></tr></table></td></tr>";
$page_old.=file_get_contents('../contents/footer.php');     
fwrite($fp_old,$page_old);
fclose($fp_old);

我的页脚页面包含一些 php 代码,见下文

<td colspan="4"><a href="../more-products.php?feat1_id=YlX>PdRf" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image49','','../images/Kluz_usa_s_over_18.jpg',1)"><img src="../images/Kluz_usa_s_18.jpg" name="Image49" width="276" height="162" border="0" alt="Universal Laser Systems VersaLASER " title="<?=$test_vra?>" ></a></td>

我想从 php 变量中分配标题、alt src 值。但结果不正确。例如。我的标题标签显示

我如何显示来自该 php 变量的实际内容

提前致谢

【问题讨论】:

    标签: php html


    【解决方案1】:

    file_get_contents 会将文件内容作为字符串提供给您,而不是将该文件解析为可执行代码。

    最简单的方法是打开输出缓冲,include 该 PHP 文件以便执行,然后将缓冲区附加到您的 $page_old 变量。

    $page_old.="<table width='1051' border='0' cellspacing='0' cellpadding='0'>";
    $page_old.="<tr><td height='50' class='tittle'>Test</td></tr>";
    $page_old.="<tr><td class='content'>My content Body</td></tr></table></td></tr>";
    
    ob_start();
    include('../contents/footer.php');
    $output = ob_get_contents();
    ob_end_clean();
    
    $page_old .= $output;
    
    file_put_contents('filename.html', $page_old);
    

    【讨论】:

    • 您可能想要删除 $page_old.=file_get_contents('../contents/footer.php');行
    • 感谢您指出这一点,我已将其删除。我希望我没看错,这很难说。
    • 谢谢。但是 PHP4 支持 file_put_contents() 吗?
    • 否,但 PHP 4 不受任何地方支持。 PHP 5 已经超过 7 年了。没有人应该运行 PHP 4,它在几年前就已经报废了。
    【解决方案2】:

    file_get_contents 返回一个带有所提供 url 的 HTML 的字符串。

    如果要使用fwrite和fclose,需要用fopen打开footer.php。

    但是你的问题对我来说没有意义。你想用$page_old 覆盖footer.php 吗?如果是这样,您可以使用file_put_contents 并执行file_put_contents('../contents/footer.php', $page_old);。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-24
      • 1970-01-01
      • 2015-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-16
      • 1970-01-01
      相关资源
      最近更新 更多