I wanted to search-and-replace in the header, but Neither @majick or @Samuel Elh answers worked for me directly. So, combining their answers I got what eventually works:
function start_wp_head_buffer() {
ob_start();
}
add_action('wp_head','start_wp_head_buffer',0);
function end_wp_head_buffer() {
$in = ob_get_clean();
// here do whatever you want with the header code
echo $in; // output the result unless you want to remove it
}
add_action('wp_head','end_wp_head_buffer', PHP_INT_MAX); //PHP_INT_MAX will ensure this action is called after all other actions that can modify head
It was added to functions.php
of my child theme.