It you're worried about nesting, then use a callback and don't call ob_end. ob_start('plugin_callback'); function plugin_callback($buffer) { return preg_replace( '|<!DOCTYPE[^>]*>|i', '*<!DOCTYPE..>*', $buffer); } And there you go. The callback will get called at end of execution. -Otto