Forum Moderators: coopster

Message Too Old, No Replies

Remove multiple blank lines.

How can i do this?

         

BlackDex

8:53 am on May 15, 2008 (gmt 0)

10+ Year Member



Hello there,

I need a way to remove/replace multiple blank lines.

Like when there are 2 or more blank lines right after eachother, i want to change them to 1 blank line.

How can i do this?

Thx in advance.

paulmadillo

10:25 am on May 15, 2008 (gmt 0)

10+ Year Member



What are you replacing the new lines in?

Is it output generated from a form or something?

I imagine what you're looking for is the 'str_replace' function.

Try this:

$yourstring = str_replace('/n/n', '/n', $yourstring);

This will replace two new line characters in $yourstring with one new line character.

StoutFiles

10:40 am on May 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



str_replace is your friend. Get to know him well.

Receptional Andy

10:46 am on May 15, 2008 (gmt 0)



If there may be two or more newlines you could use a regular expression:

preg_replace("/\n\n+/s","\n",$string);

BlackDex

1:23 pm on May 15, 2008 (gmt 0)

10+ Year Member



Thx.. i use the preg version..
Because i indeed can have 2 or more blank lines.

penders

2:14 pm on May 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Just a thought... you might need to modify your regexp slightly if your 'blank lines' contain spaces/tabs (redundant white space) and not just a singular line feed?