Input form on my website for comments. I have some silly users creating a whole lot of blank whitespace by pressing enter [space] enter [space] or lots of enters etc
In PHP, how do I preserve legit single enter or double ‘enter enter’, but remove any other combinations with spaces inbetween.
eg, This is OK:
\n\n
This is NOT:
\n\n\n...
\n\n \n...
\n \n \n...
Finally got it working and tested. I decided to break the process in two passes of replaces to keep the regex code simple (you can combine both if you desire).
First one (
/\n | {1,} | \n/) will look for any random combination of spaces and\ncharacters, leaving the solo\n.Second one (
'/(?<=\n{2})\n*/') makes use of look behind feature to match any group of\ncharacters that follows any ‘\n\n’ (double new line sequence).Testing:
Output:
I’m not a regex guru, but I think it already solves the problem decently.