I want to replace newlines in just a part of a string. Suppose I have the following:
foo bar __level [
$save = 123,
Info = '1234'
]
{Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut
labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco
laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate
velit esse cillum dolore eu fugiat nulla pariatur.}
I want to replace that to this:
foo bar __level [$save = 123,Info = '1234']
{Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut
labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco
laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate
velit esse cillum dolore eu fugiat nulla pariatur.}
So basically the newlines should be removed till it sees a { character. The rest should just keep its newlines.
I know I can replace all newlines with a preg_replace with \s+. But I dont know how to do it in this case since i just need to replace it for a small part in the string.
So how can this be done with a preg_replace?
\smatches whitespace, not just newline characters.preg_replace('#\s+#', ' ', $str);. So it would keep its spaces.