I use Notepad++ to add a space after every line in my PHP code. The problem is that it also adds a new line after the first if and while statement brackets. So it would originally look like:
if (1 == 1)
{
echo 'test';
}
while (blah = blah)
{
echo 'blah';
}
and the Notepad added new lines would turn it into:
if (1 == 1)
{
echo 'test';
}
while (blah = blah)
{
echo 'blah';
}
You can see that it adds a line before the { bracket which I do not want.
I have tried and searched but cannot figure out a pregmatch to only remove the line after the end parenthesis and the forward bracket. So that way it would look like this:
if (1 == 1)
{
echo 'test';
}
while (blah = blah)
{
echo 'blah';
}
Could anyone provide me with that pregmatch to remove the newline and spaces only between the ) and {? This is for any entire page with thousands of lines where the output is sent through one $variable so I just need the pregmatch for that one output variable to rewrite the entire code. That way I can clean up my php file code to look neater. You could pay be a million dollars and I would never figure this out on my own. I guess I am just not smart enough. Thanks.
Nothing worked and I couldn’t find anything online that matches the ) and { only. Millions of other types of examples but none of the are related.
>Solution :
You can use this regex:
\) *\n(?=\n{)
to search for a closing parenthesis ) followed by two newlines and a {, and then replace it with a ). In PHP
$text = preg_replace('/\) *\n(?=\n{)/', ")", $text);
Demo on 3v4l.org