Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ob_start in class file

Status
Not open for further replies.

stasJohn

Programmer
May 6, 2004
155
0
0
US
I need some help figuring out why the following doesn't work.

I created a class which determines what content type header to send ("text/html" or "application/xhtml+xml") depending on the browser.

If the content type is "text/html" I want to then also use preg_replace to scan the buffer to ensure that all empty closing tags have a space... so instead of "<br/>", its "<br />".

This is the structure of the class, with relevant code
Code:
class xmlmime {
  function xmlmime() { /*constructor*/ }
  function print_headers() {
    if($this->mime == 'text/html') {
      $this->search[] = '/(\S)\/>/';
      $this->replace[] = '$1 />';
    }

    ob_start("fix_code");
    header('Content-Type...);
    ...

    //end
  }
  function fix_code() {
    return preg_replace($this->search,$this->replace,$buffer);
  }
}

From a webpage I just use 1 line, "$cn = new xmlmime();"

The callback function ("fix_code") just won't work. I've verified the reg expressions and they do work. It doesn't seem like "fix_code" is actually being called. I've also tried calling ob_start like ob_start(array("this","fix_code")), but that yielded nothing.

Any ideas? Can this actually be done the way I'm trying to do it? Thanks in advance.
 
PHP manual said:
An optional output_callback function may be specified. This function takes a string as a parameter and should return a string. The function will be called when ob_end_flush() is called, or when the output buffer is flushed to the browser at the end of the request.
Don't see any flush here. Also, I assume after ob_start the code does print or echo statements, right?
 
Don't see any flush here. Also, I assume after ob_start the code does print or echo statements, right?

I don't actually use an "explicit" ob_end_flush. I just let it happen automatically when the script ends.
when the output buffer is flushed to the browser at the end of the request.


Do I actually need echo statements everywhere? I thought below was valid?

Code:
function print_header() {
		
  if($this->mime == 'text/html') {
    $this->search[] = 'br';//'/(\S)\/>/';
    $this->replace[] = 'brrrr';//'$1 />';
  }

  ob_start(array("this","fix_code"));		
  header('Content-Type: ' . $this->mime . ';charset=' . $this->charset );
  header('Vary: Accept');					
  if($this->mime == 'application/xhtml+xml') {
    echo '<?xml version="1.0" encoding="'.$this->charset.'"?>'."\n";
  }
				
}//end function

In the html doc
Code:
<?php
$cn = new xmlmime();
?>
<!DOCTYPE...
<html ...>
...
rest of html file
...
</html>

Is the output buffering actually flushed when the function print_header is done?
 
Here's an idea:
The examples in the manual with callback functions pass the variable $buffer to the callback. Your function/method definition doesn't pass anything. Could that be the reason?

AFAIK the implicit flush happens when the parser is done. Since you are using context switching mode I assume it is at the end of the entire document. It would certainly not hurt to add an ob_end_flush at the end of the file.
 
whoops. My fault. My function "fix_code" does get $buffer passed into it.

I tried adding ob_end_flush() to the end of the document and I get the ob_end_flush(): failed to delete and flush buffer. No buffer to delete or flush. error.

So, I realized that my ob_start("fix_code") didn't work. I tried ob_start(array("this","fix_code")) and that didn't work. Other combinations were tried and nothing. So I just abandoned the callback function route.

Instead, I tweaked it with the following changes and now it works.

HTML file
Code:
<?php
$cn = new xmlmime();
?>
<html ...>
rest of html file
</html>
<?php echo $cn->fix_code(); ?>

CLASS
Code:
class xmlmime {
  function xmlmime() { /*constructor*/ }
  function fix_code() {
    $html = preg_replace($this->search,$this->replace,ob_get_contents());
    ob_end_clean();
    return $html;
  }
  function print_headers() {
    if($this->mime == 'text/html') {
      $this->search[] = '/(\S)\/>/';
      $this->replace[] = '$1 />';
    }

    ob_start();
    header('Content-Type: ' . $this->mime . ';charset=' . $this->charset );
    header('Vary: Accept');					
    if($this->mime == 'application/xhtml+xml') {
      echo '<?xml version="1.0" encoding="'.$this->charset.'"?>'."\n";
    }    
  }
}

Thanks for the help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top