Ok, so I'm trying to use output buffering to make substitutions in my output for site constants, instead of hardcoding say the url everywhere I want to just put [[SITE_URL]]. The confusing this is this is working (substitutions are as desired):
<?php
define( 'DEV_MODE', TRUE);
session_start();
ob_start('ob_output');
define( 'DS', DIRECTORY_SEPARATOR);
define( 'DOC_ROOT', dirname(__FILE__));
require_once( DOC_ROOT . DS . 'includes' . DS .'initialize.php');
// Process URI
$controller = isset($_GET['controller']) ? $_GET['controller'] : DEFAULT_CONTROLLER;
$args = isset($_GET['args']) ? $_GET['args'] : NULL;
if( !is_null($args) ) {
$args = explode('/',$args);
$num = count($args);
for( $i=0;$i<$num;$i+=2 ) {
$new[$args[$i]] = $args[$i+1];
}
$args = $new;
unset($new);
}
$db = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
if( mysqli_connect_errno() ) {
echo "Connection to database failed:".mysqli_connect_errno();
exit;
}
include(INC_PATH . $controller . '.php');
$db->close();
/**
* Output buffer
*/
function ob_output($buffer) {
$header = file_get_contents(INC_PATH . 'header.inc.php');
$footer = file_get_contents(INC_PATH . 'footer.inc.php');
$output = $header . $buffer . $footer;
unset($header,$footer);
$replacements = returnConstants('SITE_');
if( !$replacements ) return FALSE;
foreach( $replacements as $k => $v ) {
$output = str_replace('[['.$k.']]',$v,$output);
}
return $output;
}
This however is not it seems to be assigning to variables properly, but the substitutions aren't happening!
<?php
ob_start('ob_output');
define( 'DEV_MODE', TRUE);
session_start();
define( 'DS', DIRECTORY_SEPARATOR);
define( 'DOC_ROOT', dirname(__FILE__));
require_once( DOC_ROOT . DS . 'includes' . DS .'initialize.php');
// Process URI
$controller = isset($_GET['controller']) ? $_GET['controller'] : DEFAULT_CONTROLLER;
$args = isset($_GET['args']) ? $_GET['args'] : NULL;
if( !is_null($args) ) {
$args = explode('/',$args);
$num = count($args);
for( $i=0;$i<$num;$i+=2 ) {
$new[$args[$i]] = $args[$i+1];
}
$args = $new;
unset($new);
}
$db = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
if( mysqli_connect_errno() ) {
echo "Connection to database failed:".mysqli_connect_errno();
exit;
}
include(INC_PATH . $controller . '.php');
$content = ob_get_clean();
ob_start('ob_output');
include(INC_PATH . 'header.inc.php');
$header = ob_get_clean();
ob_start('ob_output');
include(INC_PATH . 'footer.inc.php');
$footer = ob_get_clean();
print($header . $content . $footer);
$db->close();
/**
* Output buffer
*/
function ob_output($buffer) {
$replacements = returnConstants('SITE_');
if( !$replacements ) return FALSE;
foreach( $replacements as $k => $v ) {
$output = str_replace('[['.$k.']]',$v,$buffer);
}
return $output;
}
i can't seem to figure out why its not substituting in the second code, but it is in the first. Am I misunderstanding ob_get_clean? is it not supposed to go thru my call back before it returns the buffer? Or does it bypass the call back, in which case I need to know what to call!