You are looking for if statements that don't use curly braces, but your pattern requires curly braces.
Here is my suggestion: (Demo)
$strings = [
'if (blah === blah)
do something',
'if (foo === foo) do something',
'if (bah === bah) {
do something
}',
'if (bar === bar) {do something}'
];
foreach ($strings as $string) {
var_export(preg_match('~if\s*\(.*?\)\s*(\{)?~', $string, $m) ? $m : '');
echo "\nHas curly brace: " , isset($m[1]) ? 'Yes' : 'No';
echo "\n---\n";
}
Output:
array (
0 => 'if (blah === blah)
',
)
Has curly brace: No
---
array (
0 => 'if (foo === foo) ',
)
Has curly brace: No
---
array (
0 => 'if (bah === bah) {',
1 => '{',
)
Has curly brace: Yes
---
array (
0 => 'if (bar === bar) {',
1 => '{',
)
Has curly brace: Yes
---
Basically, use \s*
to signify where no space/newlines, a space/newline, multiple spaces/newlines may occur in the markup.
My pattern will not catch if
statements with multi-line expressions. To accommodate those fringe cases, add the s
pattern modifier to allow the .
to match newlines.