// Find all occurrences of the post items
preg_match_all($pattern, $content, $matches);
if (!empty($matches[0])) {
// We only want to replace the FIRST occurrence with our dynamic code,
// and remove the REST of the occurrences.
// Split content by the first occurrence
$pos = strpos($content, $matches[0][0]);
// Everything before the first item
$before = substr($content, 0, $pos);
// Everything after the last item
$last_pos = strpos($content, $matches[0][count($matches[0])-1]);
$after_pos = $last_pos + strlen($matches[0][count($matches[0])-1]);
$after = substr($content, $after_pos);
$new_content = $before . $replacement . $after;
file_put_contents(__DIR__ . '/blog.php', $new_content);
echo "Replaced static blogs in blog.php\n";
} else {
echo "Pattern not found.\n";
}