Connecting Tech Pros Worldwide Help | Site Map

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

  #1  
Old July 17th, 2005, 05:57 AM
Sidharta
Guest
 
Posts: n/a
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
  #2  
Old July 17th, 2005, 05:57 AM
Andy Hassall
Guest
 
Posts: n/a

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


On 23 Apr 2004 14:57:50 -0700, onlinesid@yahoo.com (Sidharta) wrote:
[color=blue]
>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);[/color]

What makes you think it doesn't work?

--
Andy Hassall <andy@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
  #3  
Old July 17th, 2005, 05:57 AM
Chung Leong
Guest
 
Posts: n/a

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


"Sidharta" <onlinesid@yahoo.com> wrote in message
news:230f566e.0404231357.f7b561a@posting.google.co m...[color=blue]
> 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);[/color]

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



  #4  
Old July 17th, 2005, 05:57 AM
Sidharta
Guest
 
Posts: n/a

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


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 <andy@andyh.co.uk> wrote in message news:<178j80lj93gfl16adkk0r71hrd00efv6da@4ax.com>. ..[color=blue]
> On 23 Apr 2004 14:57:50 -0700, onlinesid@yahoo.com (Sidharta) wrote:
>[color=green]
> >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);[/color]
>
> What makes you think it doesn't work?[/color]
  #5  
Old July 17th, 2005, 05:57 AM
Andy Hassall
Guest
 
Posts: n/a

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


On 24 Apr 2004 03:09:20 -0700, onlinesid@yahoo.com (Sidharta) wrote:
[color=blue]
>Andy Hassall <andy@andyh.co.uk> wrote in message news:<178j80lj93gfl16adkk0r71hrd00efv6da@4ax.com>. ..[color=green]
>> On 23 Apr 2004 14:57:50 -0700, onlinesid@yahoo.com (Sidharta) wrote:
>>[color=darkred]
>> >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);[/color]
>>
>> What makes you think it doesn't work?[/color]
>
>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.[/color]

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 <andy@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Closed Thread