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