You could use preg_replace()
but I think it’s much easier to overwrite the $output
via the post_gallery
filter and then remove the filter after the first run:
/**
* Remove the output of the first gallery
*
* @param string $output
* @param array $attr
* @return string $output
*/
function wpse125903_remove_the_first_gallery( $output, $attr )
{
// Run only once
remove_filter( current_filter(), __FUNCTION__ );
// Override the first gallery output
return '<!-- gallery 1 was here -->'; // Must be non-empty.
}
add_filter( 'post_gallery', 'wpse125903_remove_the_first_gallery', 10, 2 );
This should remove only the first gallery in your posts.