You can use
(?s)^\s*<\?php(?!.*\?>\s*$).*+$
See demo
Regex explanation:
(?s) - Enable singleline mode for the whole pattern for . to match newline
^ - Start of string
\s* - Optional whitespace, 0 or more repetitions
<\?php - Literal <?php
(?!.*\?>\s*$) - Look-ahead checking if the string does not end with ?>whitespace
.*+$ - Matches without backtracking any characters up to the string end.
The possessive quantifier (as in .*+) enables us to consume characters once, in 1 go, and never come back in search of possible permutations.
Possessive quantifiers are a way to prevent the regex engine from
trying all permutations. This is primarily useful for performance
reasons.
And we do not to use explicit SKIP-FAIL verbs then.
trim()the string first, this gets a lot easier and could be done with simplestr_poscalls