Replacing empty lines at the beginning of a php file


I’m really not getting my mind around regular expressions so I would like to ask for your help on this.

After removing the eval64 hack from several WP files, these contain some empty lines before the opening <?php tag, causing a “Header already sent” error. What I would like to achieve is:

Change from:




What regular expression would I use here?

Many-many thanks for your help in advance!

Thanks joebert! That worked like a charm!!!

preg_replace('#^\\s+<\\?#s', '<?', $file_contents);