I'm trying to build a regex that will use preg_replace
to quote the right side of an expression if it's unquoted.
So myvar = 3
becomes myvar = '3'
. It should only deal with unquoted strings that are contiguous (so if there any spaces on the first string need be quoted e.g. myvar = 3 5
will become myvar = '3' 5
).
I also want it to ignore any quoted string, so myvar = 'this is quoted'
should not be modified.
So far I have the code below:
$str = 'myvar = 3';
$regex = '/([\w\@\-]+) *(\=|\>|\>\=|\<|\<\=) *([\w]+)/i';
$replace = '\1 \2 \'\3\'';
$result = preg_replace($regex, $replace_str, $str);
What do I need to put in $regex
to make this work?