get php dynamically generated html code as a string
-
11-06-2021 - |
문제
This is generic PHP question not just oscommerce.
In the old version of oscommerce a column was displayed by creating a object of a PHP class. If i include this it gets displayed
new infoBox2($info_box_contents) ;
But in new version i don't have the liberty of doing this because of a lot of reasons. i MUST return all the HTML code generated by creating the above object as a string. This is what the string is right now -
$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>';
You can notice it is predefined HTML code. I need to change the $data value to the dynamically generated HTML code generated when the object is created. Any ideas on how to do this? i tried type casting the object as a string and using var_dump
var_dump is giving something like this
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"" }
which is not exactly the HTML code for it.
All that matters here is getting the HTML code generated by the PHP code as a string.HOW DO I DO IT?
If i place the new object creation in a seperate file and use file_get_contents then will it return the PHP code itself or will it return the HTML code generated by the php. Note here that i will be passing the filepath NOT a URL. I can't pass the URL because of oscommerce internals which i will not go in depth now. i will use something Like this :-
file_get_contents("myfile.php");
NOT
file_get_contents("http://mywebsite.com/myfile.php");
해결책
You can use the technique called "output buffering".
# 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();