how to remove multiple line breaks?

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

    how to remove multiple line breaks?

    I'm am trying to remove multiple line breaks ("\n"). Here's my code:

    $imageStr = trim($_POST['images']);
    $imageStr = str_replace("\n \n", "\n", $imageStr);

    ----------

    $_POST['images'] is a textarea field, and I am trying to remove extra
    hard returns that a user types into the text area.

    When I change the code to:
    $imageStr = str_replace("\n ", "%", $imageStr);
    it works fine. So I know the function works and I have the right
    character ("\n").

    Anyone know what I'm doing wrong?
    thanks much,
    jon
    jlarosa at alumni dot brown dot edu

  • Ken Robinson

    #2
    Re: how to remove multiple line breaks?


    Jon LaRosa wrote:[color=blue]
    > I'm am trying to remove multiple line breaks ("\n"). Here's my code:
    >
    > $imageStr = trim($_POST['images']);
    > $imageStr = str_replace("\n \n", "\n", $imageStr);
    >
    > ----------
    >
    > $_POST['images'] is a textarea field, and I am trying to remove extra
    > hard returns that a user types into the text area.
    >
    > When I change the code to:
    > $imageStr = str_replace("\n ", "%", $imageStr);
    > it works fine. So I know the function works and I have the right
    > character ("\n").[/color]

    How do you know it's NOT working??

    What happens if you do $imageStr = str_replace("\n \n", "%",
    $imageStr);?

    Do two "\n"s get replaced by one "%"?

    Ken

    Comment

    • Pedro Graca

      #3
      Re: how to remove multiple line breaks?

      Jon LaRosa wrote:[color=blue]
      > I'm am trying to remove multiple line breaks ("\n"). Here's my code:
      >
      > $imageStr = trim($_POST['images']);[/color]

      /* while there are double line breaks in $imageStr */
      while (strpos($imageS tr, "\n\n") !== false) {

      /* replace one such double line break by a single one */[color=blue]
      > $imageStr = str_replace("\n \n", "\n", $imageStr);[/color]

      }

      --
      Mail to my "From:" address is readable by all at http://www.dodgeit.com/
      == ** ## !! ------------------------------------------------ !! ## ** ==
      TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
      may bypass my spam filter. If it does, I may reply from another address!

      Comment

      • Justin Koivisto

        #4
        Re: how to remove multiple line breaks?

        Jon LaRosa wrote:
        [color=blue]
        > I'm am trying to remove multiple line breaks ("\n"). Here's my code:
        >
        > $imageStr = trim($_POST['images']);
        > $imageStr = str_replace("\n \n", "\n", $imageStr);
        >
        > ----------
        >
        > $_POST['images'] is a textarea field, and I am trying to remove extra
        > hard returns that a user types into the text area.
        >
        > When I change the code to:
        > $imageStr = str_replace("\n ", "%", $imageStr);
        > it works fine. So I know the function works and I have the right
        > character ("\n").
        >
        > Anyone know what I'm doing wrong?
        > thanks much,
        > jon
        > jlarosa at alumni dot brown dot edu
        >[/color]

        $string=preg_re place('`[\r\n]+`',"\n",$strin g);

        --
        Justin Koivisto - spam@koivi.com

        Comment

        • Michael Fesser

          #5
          Re: how to remove multiple line breaks?

          .oO(Pedro Graca)
          [color=blue]
          >/* while there are double line breaks in $imageStr */
          >while (strpos($imageS tr, "\n\n") !== false) {[/color]

          IMHO it would be much easier to use a regular expression instead.

          Micha

          Comment

          • Pedro Graca

            #6
            Re: how to remove multiple line breaks?

            Pedro Graca wrote:[color=blue]
            > /* while there are double line breaks in $imageStr */
            > while (strpos($imageS tr, "\n\n") !== false) {
            >
            > /* replace one such double line break by a single one */[/color]

            Oops: wrong comment. Should have been:

            /* replace as many such double line breaks as possible
            by a single one */[color=blue][color=green]
            >> $imageStr = str_replace("\n \n", "\n", $imageStr);[/color]
            >
            > }[/color]


            --
            Mail to my "From:" address is readable by all at http://www.dodgeit.com/
            == ** ## !! ------------------------------------------------ !! ## ** ==
            TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
            may bypass my spam filter. If it does, I may reply from another address!

            Comment

            • Chung Leong

              #7
              Re: how to remove multiple line breaks?

              "Jon LaRosa" <jlarosa@alumni .brown.edu> wrote in message
              news:1102618971 .322599.173990@ f14g2000cwb.goo glegroups.com.. .[color=blue]
              > I'm am trying to remove multiple line breaks ("\n"). Here's my code:
              >
              > $imageStr = trim($_POST['images']);
              > $imageStr = str_replace("\n \n", "\n", $imageStr);
              >
              > ----------
              >
              > $_POST['images'] is a textarea field, and I am trying to remove extra
              > hard returns that a user types into the text area.
              >
              > When I change the code to:
              > $imageStr = str_replace("\n ", "%", $imageStr);
              > it works fine. So I know the function works and I have the right
              > character ("\n").
              >
              > Anyone know what I'm doing wrong?
              > thanks much,
              > jon
              > jlarosa at alumni dot brown dot edu[/color]

              Linefeeds coming from the browser are "\r\n", which is why your replacement
              code doesn't work. Use the snippet from Justin.


              Comment

              Working...