');
$endPos = strpos($response, '');
// Check if the starting string is found
if ($startPos !== false) {
// Extract the content between the starting and ending strings
$content = substr($response, $startPos, $endPos - $startPos);
// Convert image URLs to base64
$content = preg_replace_callback('/(
]+src=")([^"]+)("[^>]*>)/i', function ($matches) {
$imageUrl = $matches[2];
$imageData = base64_encode(file_get_contents($imageUrl));
return $matches[1] . 'data:image/png;base64,' . $imageData . $matches[3];
}, $content);
// Output the extracted content with base64-encoded images
echo $content;
} else {
// Find the position of the fallback string
$fallbackStartPos = strpos($response, '');
$fallbackEndPos = strpos($response, '
', $fallbackStartPos);
// Check if the fallback string is found
if ($fallbackStartPos !== false && $fallbackEndPos !== false) {
// Extract the fallback content
$fallbackContent = substr($response, $fallbackStartPos, $fallbackEndPos - $fallbackStartPos + 8);
// Convert image URLs to base64
$fallbackContent = preg_replace_callback('/(
]+src=")([^"]+)("[^>]*>)/i', function ($matches) {
$imageUrl = $matches[2];
$imageData = base64_encode(file_get_contents($imageUrl));
return $matches[1] . 'data:image/png;base64,' . $imageData . $matches[3];
}, $fallbackContent);
// Output the fallback content with base64-encoded images
echo $fallbackContent;
} else {
// If both strings are not found, display an error message
echo "Content not found.";
}
}
}
?>