php - 获取php动态生成的html代码作为字符串

标签 php html oscommerce

这是一般的 PHP 问题,而不仅仅是 oscommerce。

在旧版本的 oscommerce 中,通过创建 PHP 类的对象来显示列。如果我包含它,它就会显示

new infoBox2($info_box_contents) ;

但在新版本中,由于很多原因,我没有这样做的自由。我必须将通过创建上述对象生成的所有 HTML 代码作为字符串返回。这就是现在的字符串 -

$data = '<div class="ui-widget infoBoxContainer">' .'  <div class="ui-widget-header infoBoxHeading">' . MODULE_BOXES_CATEGORIES_BOX_TITLE . '</div>' .'  <div class="ui-widget-content infoBoxContents">' . $categories_string . '</div>' .'</div>';

您会注意到它是预定义的 HTML 代码。我需要将 $data 值更改为创建对象时生成的动态生成的 HTML 代码。 关于如何做到这一点的任何想法?我尝试将对象类型转换为字符串并使用 var_dump

var_dump 给出了这样的东西

object(infoBox2)#8 (7) { ["table_border"]=> string(1) "0" ["table_width"]=> string(4) "100%" ["table_cellspacing"]=> string(1) "0" ["table_cellpadding"]=> string(1) "0" ["table_parameters"]=> string(22) " class="infoBox_table"" ["table_row_parameters"]=> string(0) "" ["table_data_parameters"]=> string(19) " class="infoBox_td"" }

这不完全是它的 HTML 代码。

这里重要的是将 PHP 代码生成的 HTML 代码作为字符串获取。我该怎么做?

如果我将新对象创建放在一个单独的文件中并使用 file_get_contents 那么它会返回 PHP 代码本身还是返回由 php.ini 生成的 HTML 代码。请注意,我将传递文件路径而不是 URL。由于 oscommerce 内部机制,我无法传递 URL,现在我不会深入探讨。我会用这样的东西:-

file_get_contents("myfile.php");

file_get_contents("http://mywebsite.com/myfile.php");

最佳答案

您可以使用称为“输出缓冲”的技术。

# start redirecting output to a buffer
ob_start();

# execute the other PHP file
include('myfile.php');

# grab whatever got output since ob_start() (and stop buffering)
$html = ob_get_clean();

关于php - 获取php动态生成的html代码作为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10789238/

相关文章:

paypal - Oscommerce PayPal IPN 仍处于待定状态

magento - 如何将 osCommerce 商店导入 Magento 1.7.0.2

php - 如何使用 PHP 脚本打开 protected pdf

php - 带微秒的日期时间

php - oscommerce tep_db_perform -- 更新

html - 鹈鹕网站生成器 : themes do not have visible table borders?

html - 不同定位元素的 z-index

php - 如何从 WordPress 获取所有基本(父或根)类别

php - 与 Laravel 一对一聊天

javascript - 点击显示表单,平滑过渡