A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2009 yearRemove excessive line returns - page 1
User InfoPosts
Remove excessive line returns#1
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 input.

Sample input

i like cats

my cat is happy
i love my cat

hope you have a nice day

Desired output

i like cats

my cat is happy
i love my cat

hope you have a nice day

I am not seeing anything built in, and a string replace would take many iterations of it to do the work. Before I whip up a small recursive string replace, I wanted to see what other suggestions you all had.

I have an odd feeling there is a regex for this one as well.

posted date: 2009-04-07 21:40:00

Re: Remove excessive line returns#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2009-04-07 21:40:01

Re: Remove excessive line returns#3
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.

posted date: 2009-04-07 21:55:00

Re: Remove excessive line returns#4
function str_squeeze($body) { return preg_replace("/\n\n+/", "\n\n", $body);}

posted date: 2009-04-07 22:00:00

Re: Remove excessive line returns#5
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. ;)

posted date: 2009-04-07 22:13:00

Re: Remove excessive line returns#6
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.

posted date: 2009-04-07 23:01:00

Re: Remove excessive line returns#7
Not much, an emails worth for a user who sends in a email, it is part of a web system.

posted date: 2009-04-08 08:09:00

Re: Remove excessive line returns#8
This returns all lines separated by one \n, after I change ' to " in the args.

posted date: 2009-04-08 11:56:00

Re: Remove excessive line returns#9
It might be a good idea to add checking for \w - perhaps \w? to account for when there is whitespace interspersed. People who add random newlines might also add random spaces and tabs.

posted date: 2009-04-08 13:07:00

Re: Remove excessive line returns#10
To consider all three line break sequences:preg_replace((/(?:\r\n|[\r\n]){2,}/(, "\n\n", $str)

posted date: 2009-04-08 23:57:00

select page: « 1 »
Copyright ©2008-2017 www.momige.com, all rights reserved.