help removing new line chars

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

    help removing new line chars

    Hello,

    i have a string that was entered in a web form and stored in a mysql
    database where the user entered carriage returns as well as <br>'s
    after each line in the form. the data is stored as

    line 1<br>
    line 2<br>
    line 3

    i need is to modify the text so instead it reads

    line1<br>line2< br>line3

    the problem is when i use strpos() and substr() on the multi line
    text, i keep getting these invisible \n chars that i can't seem to get
    rid of. even after using trim($string), i still can't get rid of these
    buggers.

    is there a function in php that strips the invisible \n chars that are
    generated from a web form?

    Thanks so much for any suggestions :)

    Aaron;
  • Chris Hope

    #2
    Re: help removing new line chars

    Aaron Collins wrote:
    [color=blue]
    > i have a string that was entered in a web form and stored in a mysql
    > database where the user entered carriage returns as well as <br>'s
    > after each line in the form. the data is stored as
    >
    > line 1<br>
    > line 2<br>
    > line 3
    >
    > i need is to modify the text so instead it reads
    >
    > line1<br>line2< br>line3
    >
    > the problem is when i use strpos() and substr() on the multi line
    > text, i keep getting these invisible \n chars that i can't seem to get
    > rid of. even after using trim($string), i still can't get rid of these
    > buggers.
    >
    > is there a function in php that strips the invisible \n chars that are
    > generated from a web form?[/color]

    IIRC, web forms are supposed to submit line breaks as \r\n so what you can
    do to ensure all are removed is something like so:

    $text = str_replace("\r ", "", $text);
    $text = str_replace("\n ", "", $text);

    or the nasty one liner

    $text = str_replace("\r ", "", str_replace("\n ", "", $text));

    You can also do this:

    $text = str_replace("\r \n", "", $text);

    but if you do have any line breaks that are just "\n" then it won't get rid
    of them.

    Note that trim() only trims at the beginning and end of the string, not of
    lines in the string, hence my solution.

    --
    Chris Hope
    The Electric Toolbox - http://www.electrictoolbox.com/

    Comment

    • Pedro Graca

      #3
      Re: help removing new line chars

      Chris Hope wrote:[color=blue]
      > Aaron Collins wrote:[color=green]
      >> is there a function in php that strips the invisible \n chars that are
      >> generated from a web form?[/color]
      >
      > IIRC, web forms are supposed to submit line breaks as \r\n so what you can
      > do to ensure all are removed is something like so:
      >
      > $text = str_replace("\r ", "", $text);
      > $text = str_replace("\n ", "", $text);
      >
      > or the nasty one liner
      >
      > $text = str_replace("\r ", "", str_replace("\n ", "", $text));[/color]

      or the elegant one liner :)

      $text = str_replace(arr ay("\r", "\n"), "", $text);

      --
      USENET would be a better place if everybody read: : mail address :
      http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
      http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
      http://www.expita.com/nomime.html : to 10K bytes :

      Comment

      • Chris Hope

        #4
        Re: help removing new line chars

        Pedro Graca wrote:
        [color=blue]
        > Chris Hope wrote:[color=green]
        >> Aaron Collins wrote:[color=darkred]
        >>> is there a function in php that strips the invisible \n chars that are
        >>> generated from a web form?[/color]
        >>
        >> IIRC, web forms are supposed to submit line breaks as \r\n so what you
        >> can do to ensure all are removed is something like so:
        >>
        >> $text = str_replace("\r ", "", $text);
        >> $text = str_replace("\n ", "", $text);
        >>
        >> or the nasty one liner
        >>
        >> $text = str_replace("\r ", "", str_replace("\n ", "", $text));[/color]
        >
        > or the elegant one liner :)
        >
        > $text = str_replace(arr ay("\r", "\n"), "", $text);[/color]

        Oh cool, I didn't know you could do that until you pointed it out. I guess
        that's what I get for using PHP for such a long time. According to the
        manual the ability to pass arrays was added in 4.0.5

        --
        Chris Hope
        The Electric Toolbox - http://www.electrictoolbox.com/

        Comment

        • Aaron Collins

          #5
          Re: help removing new line chars

          Chris Hope <chris@electric toolbox.com> wrote in message news:<108372880 1_9571@news.ath enanews.com>...[color=blue]
          > Aaron Collins wrote:
          >[color=green]
          > > i have a string that was entered in a web form and stored in a mysql
          > > database where the user entered carriage returns as well as <br>'s
          > > after each line in the form. the data is stored as
          > >
          > > line 1<br>
          > > line 2<br>
          > > line 3
          > >
          > > i need is to modify the text so instead it reads
          > >
          > > line1<br>line2< br>line3
          > >
          > > the problem is when i use strpos() and substr() on the multi line
          > > text, i keep getting these invisible \n chars that i can't seem to get
          > > rid of. even after using trim($string), i still can't get rid of these
          > > buggers.
          > >
          > > is there a function in php that strips the invisible \n chars that are
          > > generated from a web form?[/color]
          >
          > IIRC, web forms are supposed to submit line breaks as \r\n so what you can
          > do to ensure all are removed is something like so:
          >
          > $text = str_replace("\r ", "", $text);
          > $text = str_replace("\n ", "", $text);
          >
          > or the nasty one liner
          >
          > $text = str_replace("\r ", "", str_replace("\n ", "", $text));
          >
          > You can also do this:
          >
          > $text = str_replace("\r \n", "", $text);
          >
          > but if you do have any line breaks that are just "\n" then it won't get rid
          > of them.
          >
          > Note that trim() only trims at the beginning and end of the string, not of
          > lines in the string, hence my solution.[/color]

          The str_replace() worked!! Thank you so much for your help :) I was
          really having fun with those suckers. I didn't know about the \r.
          Thanks for taking the time and helping me out. I really appreciate it.

          Aaron;

          Comment

          • R. Rajesh Jeba Anbiah

            #6
            Re: help removing new line chars

            aaron@smartwebd evelopment.com (Aaron Collins) wrote in message news:<f1ea5874. 0405041931.78e6 1b12@posting.go ogle.com>...[color=blue]
            > Hello,
            >
            > i have a string that was entered in a web form and stored in a mysql
            > database where the user entered carriage returns as well as <br>'s
            > after each line in the form. the data is stored as
            >
            > line 1<br>
            > line 2<br>
            > line 3
            >
            > i need is to modify the text so instead it reads
            >
            > line1<br>line2< br>line3[/color]

            <snip>

            //strip newlines and leading spaces... logic from Smarty code (but, order reversed)
            $strip_search = array('%[\r\n]+%m', // remove CRs and newlines
            "![\t ]+$|^[\t ]+!m"); // remove leading/trailing space chars
            $strip_replace = array('',
            '');
            $page = preg_replace($s trip_search, $strip_replace, $page);

            --
            | Just another PHP saint |
            Email: rrjanbiah-at-Y!com

            Comment

            • Psyphen

              #7
              Re: help removing new line chars

              Aaron Collins wrote:[color=blue]
              > Hello,
              >
              > i have a string that was entered in a web form and stored in a mysql
              > database where the user entered carriage returns as well as <br>'s
              > after each line in the form. the data is stored as
              >
              > line 1<br>
              > line 2<br>
              > line 3
              >
              > i need is to modify the text so instead it reads
              >
              > line1<br>line2< br>line3
              >
              > the problem is when i use strpos() and substr() on the multi line
              > text, i keep getting these invisible \n chars that i can't seem to get
              > rid of. even after using trim($string), i still can't get rid of these
              > buggers.
              >
              > is there a function in php that strips the invisible \n chars that are
              > generated from a web form?
              >
              > Thanks so much for any suggestions :)
              >
              > Aaron;[/color]

              The easiest way in my opinion is: strip_tags( $text);

              This will strip all tags from the string.

              Comment

              Working...