Catch “?>” near eof with regex

The php closing tag “?>” near end fo file can cause unwanted output to be sent before if followed by some spaces and/or newlines. Here is a regex I made to find?>near eof…

The regex

\?>(?s:.){0,10}\Z

The breakdown

\?escapes the ? since that is a special char
(?s:.) catch any character including newlines
{0,10} …0 to 10 of those
\Z match end of string ignoring newlines

For the (?s:.) discussion, see http://stackoverflow.com/questions/8303488/regex-to-match-any-character-including-new-lines

Allow a .php file to end with “?>”

… just change “0” to “1”:

\?>(?s:.){1,10}\Z

The Why

For your favorite IDE, to prevent PHP from doing funny stuff (most ofter “Cannot modify header information – headers already sent by somefile on a_line”) .

catch_php_closing_tag_near_eof