ob_get_clean

ob_get_clean(): string|false

Returns the current buffer contents and ends buffering — equivalent to ob_get_contents plus ob_end_clean.

C C23

C23
#include <stdio.h>
#include <string.h>

int main(void) {
    // Write several segments, fetch once: sprintf appends into the char array
    char buffer[64] = "";
    sprintf(buffer + strlen(buffer), "line 1\n");
    sprintf(buffer + strlen(buffer), "line 2\n");
    printf("[%s]end", buffer);
    return 0;
}
Output
[line 1
line 2
]end

C++ C++26

C++26
#include <iostream>
#include <sstream>

int main() {
    // Write several segments, fetch once: << appends into the ostringstream
    std::ostringstream buf;
    buf << "line 1\n";
    buf << "line 2\n";
    std::cout << "[" << buf.str() << "]end";
    return 0;
}
Output
[line 1
line 2
]end

C# v11.0.100

v11.0.100
using System.IO;

// Write several segments, fetch once: ToString reads out; StringBuilder is the internal buffer
var sw = new StringWriter();
sw.Write("line 1\n");
sw.Write("line 2\n");
string captured = sw.ToString();
Console.Write($"[{captured}]end");
Output
[line 1
line 2
]end

Go v1.27.4

v1.27.4
package main

import (
	"bytes"
	"fmt"
	"os"
)

func main() {
	// Write several segments, fetch once: String reads out, Reset empties
	var buf bytes.Buffer
	fmt.Fprintln(&buf, "line 1")
	buf.WriteString("line 2\n")
	captured := buf.String()
	fmt.Fprintf(os.Stdout, "[%s]end", captured)
}
Output
[line 1
line 2
]end

Java v27.0.1

v27.0.1
import java.io.ByteArrayOutputStream;

public class Main {
    public static void main(String[] args) {
        // Write several segments, fetch once: toString reads out, reset empties
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        baos.writeBytes("line 1\n".getBytes());
        baos.writeBytes("line 2\n".getBytes());
        String captured = baos.toString();
        System.out.printf("[%s]end", captured);
    }
}
Output
[line 1
line 2
]end

JavaScript v26.13.1

v26.13.1
// Write several segments, fetch once: concatenation IS the buffer
let captured = "";
captured += "line 1\nline 2\n";
process.stdout.write(`[${captured}]end`);
Output
[line 1
line 2
]end

PHP v8.6.0

v8.6.0
// ob_get_clean = ob_get_contents + ob_end_clean: fetch the content and close the buffer
ob_start();
echo "line 1\nline 2\n";
$captured = ob_get_clean();
echo "[", $captured, "]";
// The buffer is closed: nesting level back to 0
echo "level=", ob_get_level();
Output
[line 1
line 2
]level=0

Python v3.15.2

v3.15.2
import io

# Write several segments, fetch once: getvalue keeps the buffer open — discard buf yourself
buf = io.StringIO()
buf.write("line 1\nline 2\n")
captured = buf.getvalue()
buf.close()
print(f"[{captured}]end", end="")
Output
[line 1
line 2
]end

Rust v1.99.1

v1.99.1
use std::fmt::Write;

fn main() {
    // Write several segments, fetch once: the string IS the buffer
    let mut buffer = String::new();
    writeln!(buffer, "line 1").unwrap();
    buffer.push_str("line 2\n");
    print!("[{buffer}]end");
}
Output
[line 1
line 2
]end

TypeScript v26.13.1

v26.13.1
// Write several segments, fetch once: concatenation IS the buffer
let captured = "";
captured += "line 1\nline 2\n";
process.stdout.write(`[${captured}]end`);
Output
[line 1
line 2
]end

Language differences

C: sprintf appending at the strlen offset emulates multi-segment writes; buffer overflow is the programmer's responsibility (use snprintf in production).

C++: multiple << into an ostringstream, then str() retrieves once.

C#: multiple Write into a StringWriter, then ToString.

Go: multiple Write/Fprintln into a bytes.Buffer, then String() to retrieve and Reset() to clear (the counterpart of end_clean).

Java: multiple writeBytes into a ByteArrayOutputStream, then toString; reset clears.

JavaScript: no buffering levels — accumulate segments and consume once; the Node demo uses process.stdout.write to avoid a trailing newline.

PHP: ob_get_clean is a combined read-and-end, equivalent to ob_get_contents() + ob_end_clean(); buffers nest (ob_get_level reports depth) — after retrieval this demo is back at level 0.

Python: getvalue reads without closing, so an explicit close is needed — there is no one-step read-and-close compound API like PHP's.

Rust: writeln!/push_str append segments into a String; print! avoids an extra newline on output.

TypeScript: same as JavaScript.