How can PHP be used to ensure that graphics are properly included in a generated DOC file when using ob_get_contents()?

When using ob_get_contents() to capture the output of HTML content that includes graphics, the graphics may not be properly included in the generated DOC file. To ensure that the graphics are included, you can use output buffering to capture the HTML content, replace the image URLs with base64 encoded images, and then output the modified content to the DOC file.

<?php
ob_start();
?>
<!DOCTYPE html>
<html>
<head>
    <title>Example</title>
</head>
<body>
    <h1>Example</h1>
    <img src="image.jpg" alt="Image">
</body>
</html>
<?php
$html = ob_get_contents();
ob_end_clean();

// Replace image URLs with base64 encoded images
$html = preg_replace_callback('/<img src="([^"]+)"[^>]*>/', function($match) {
    $image = file_get_contents($match[1]);
    $base64 = base64_encode($image);
    return '<img src="data:image/jpeg;base64,'.$base64.'" />';
}, $html);

// Output modified content to DOC file
file_put_contents('output.doc', $html);
?>