I am looking for something like trim() but for within the bounds of a string. Users sometimes put 2, 3, 4, or more line returns after they type, I need to sanitize this inp
To consider all three line break sequences:
preg_replace('/(?:\r\n|[\r\n]){2,}/', "\n\n", $str)
Finally managed to get it, needs preg so you are using the PCRE version in php, and also needs a \n\n replacement string, in order to not wipe all line endings but one:
$body = preg_replace("/\n\n+/", "\n\n", $body);
Thanks for getting me on the right track.
How much text do you need to do this on? If it is less than about 100k then you could probably just use a simple search and replace regex (searching something like /\n+/
and replace with \n
)
On the other hand if you need to go through megabytes of data, then you could parse the text character by character, copying the input to the output, except when mulitple newlines are encountered, in which case you would just copy one newline and ignore the rest.
I would not recommend a recursive string replace though, sounds like that would be very very slow.
The following regular expression should remove multiple linebreaks while ignoring single line breaks, which are okay by your definition:
ereg_replace("\n\n+", "\n\n", $string);
You can test it with this PHP Regular Expression test tool, which is very handy (but as it seems not in perfect parity with PHP).
[EDIT] Fixed the ' to ", as they didn't seem to work. Have to admit I just tested the regex in the web tool. ;)
function str_squeeze($body) {
return preg_replace("/\n\n+/", "\n\n", $body);
}