Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Removing new lines after parentheses and forward bracket?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading