468,780 Members | 2,330 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,780 developers. It's quick & easy.

please help with preg_replace how to get rid of extra new lines? I've tried so many ways

Hi all,

how come this doesn't work?????

# convert to unix new lines
$text = preg_replace("/\r\n/", "\n", $text);
# remove extra new lines
$text = preg_replace("/\n+/", "\n", $text);

is there better ways to remove extra new lines???

regards,

Sid
Jul 17 '05 #1
4 9936
On 23 Apr 2004 14:57:50 -0700, on*******@yahoo.com (Sidharta) wrote:
how come this doesn't work?????

# convert to unix new lines
$text = preg_replace("/\r\n/", "\n", $text);
# remove extra new lines
$text = preg_replace("/\n+/", "\n", $text);


What makes you think it doesn't work?

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Jul 17 '05 #2
"Sidharta" <on*******@yahoo.com> wrote in message
news:23*************************@posting.google.co m...
Hi all,

how come this doesn't work?????

# convert to unix new lines
$text = preg_replace("/\r\n/", "\n", $text);
# remove extra new lines
$text = preg_replace("/\n+/", "\n", $text);


I usually do preg_replace('/[\r\n]+/', "\n", $text), mainly because I can't
remember which one comes first, \n or \r.

Jul 17 '05 #3
Because I have tried it. Infact, many other ways too. The text came
from a html file, loaded into a variable. Then I tried to remove extra
new lines using those preg calls. But everytime I looked at the
result, those extra new lines still there.

load the string from file using this function:

# read the specified file and return the content
function getTemplateString($file_path) {

# get contents of a file into a string
$handle = @fopen($file_path, 'r');
if (!$handle) {
# return empty if error
return '';
}
$contents = fread($handle, filesize($file_path));
fclose ($handle);

# return the content of the file
return $contents;
}
Andy Hassall <an**@andyh.co.uk> wrote in message news:<17********************************@4ax.com>. ..
On 23 Apr 2004 14:57:50 -0700, on*******@yahoo.com (Sidharta) wrote:
how come this doesn't work?????

# convert to unix new lines
$text = preg_replace("/\r\n/", "\n", $text);
# remove extra new lines
$text = preg_replace("/\n+/", "\n", $text);


What makes you think it doesn't work?

Jul 17 '05 #4
On 24 Apr 2004 03:09:20 -0700, on*******@yahoo.com (Sidharta) wrote:
Andy Hassall <an**@andyh.co.uk> wrote in message news:<17********************************@4ax.com>. ..
On 23 Apr 2004 14:57:50 -0700, on*******@yahoo.com (Sidharta) wrote:
>how come this doesn't work?????
>
># convert to unix new lines
>$text = preg_replace("/\r\n/", "\n", $text);
># remove extra new lines
>$text = preg_replace("/\n+/", "\n", $text);


What makes you think it doesn't work?


Because I have tried it. Infact, many other ways too. The text came
from a html file, loaded into a variable. Then I tried to remove extra
new lines using those preg calls. But everytime I looked at the
result, those extra new lines still there.


But I tried it too before posting, it looks like it works to me. I'd put the
regexp itself in single quotes, but it works in doubles with literal newline
characters in the expression anyway.

<pre>
<?php
function hexdump($data)
{
for ($i=0; $i<strlen($data); $i++)
{
printf("%02x ", ord($data{$i}));

}
print "\n";
}

$text = "a\r\n\r\n\r\nb\r\n";
hexdump($text);

# convert to unix new lines
$text = preg_replace("/\r\n/", "\n", $text);
# remove extra new lines
$text = preg_replace("/\n+/", "\n", $text);

hexdump($text);
?>
</pre>

Output:

61 0d 0a 0d 0a 0d 0a 62 0d 0a
61 0a 62 0a

You mention HTML - are you sure you want to get rid of \n newlines; perhaps
you need to remove <br> linebreaks?

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Murat Tasan | last post: by
reply views Thread by zhoujie | last post: by
2 posts views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.