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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sidharta

    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
  • Andy Hassall

    #2
    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

    Comment

    • Chung Leong

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

      "Sidharta" <onlinesid@yaho o.com> wrote in message
      news:230f566e.0 404231357.f7b56 1a@posting.goog le.com...[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.



      Comment

      • Sidharta

        #4
        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 getTemplateStri ng($file_path) {

        # get contents of a file into a string
        $handle = @fopen($file_pa th, '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:<178j80lj9 3gfl16adkk0r71h rd00efv6da@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]

        Comment

        • Andy Hassall

          #5
          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:<178j80lj9 3gfl16adkk0r71h rd00efv6da@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

          Comment

          Working...