Home > Article > Backend Development > Output control class_PHP tutorial
/**
*
* Author: Xu Zuning (Nagging)
* Email: czjsz_ah@stats.gov.cn
* Developed: 2002.07
*
*
* Category: outbuffer
* Function: Encapsulate some output control functions and control the output object.
*<*>*Method:
*Run ($ PROC) Run the PHP program
*$ PROC PHP program name
*Display () Output operation
*Savetofile ($ FILENAME) Save the running results to a file, which can generally be used to generate static pages
* $filename file name
* loadfromfile($filename) Load the saved file
* $filename file name
*
* Example:
* 1.
* require_once "outbuffer.php";
* $out = new outbuffer();
* $out->run("test.php");
* $out->display();
*
* 2.
* require_once "outbuffer.php";
* require_once "outbuffer.php";
* $out = new outbuffer("test.php");
* $out->savetofile("temp.htm");
*
* 3.
* require_once "outbuffer.php";
* $out = new outbuffer();
* $out->loadfromfile("temp.htm");
* $out->display();
*
*/
class outbuffer {
var $length;
var $buffer;
function outbuffer($proc="") {
$this->run($proc);
}
function run($proc="") {
ob_start();
include($proc);
$this->length = ob_get_length();
$this->buffer = ob_get_contents();
$this->buffer = eregi_replace("r?n","rn",$this->buffer);
ob_end_clean();
}
function display() {
echo $this->buffer;
}
function savetofile($filename="") {
if($filename == "") return;
$fp = fopen($filename,"w");
fwrite($fp,$this->buffer);
fclose($fp);
}
function loadfromfile($filename="") {
if($filename == "") return;
$fp = fopen($filename,"w");
$this->buffer = fread($fp,filesize($filename));
fclose($fp);
}
}
?>