7 Replies Latest reply on May 18, 2006 12:17 PM by Newsgroup_User

    - How do you refer to an HTML carriage return in PHP?

    Level 7
      Someone sent me a php script that replaces foul language with kinder words
      in message board postings. I'd like the same script to weed out carriage
      returns, but we can't find the correct syntax for it.

      We're using :
      $patterns[0] = '/\n\r/';

      What's the correct syntax to use in this situation?

      Thanks!


        • 1. Re: - How do you refer to an HTML carriage return in PHP?
          Level 7
          Reese wrote:
          > We're using :
          > $patterns[0] = '/\n\r/';
          >
          > What's the correct syntax to use in this situation?

          That looks for newline followed by carriage return. It's normally the
          other way round. A more comprehensive way is to use a character class
          like this:

          $patterns[0] = '/[\n\r]/';

          That searches for either a newline character or a carriage return.

          --
          David Powers
          Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
          Author, "Foundation PHP 5 for Flash" (friends of ED)
          http://foundationphp.com/
          • 2. Re: - How do you refer to an HTML carriage return in PHP?
            Level 7
            "David Powers" <david@example.com> wrote in message
            news:e4g5ki$bup$1@forums.macromedia.com...
            >
            > That looks for newline followed by carriage return. It's normally the
            > other way round. A more comprehensive way is to use a character class like
            > this:
            >
            > $patterns[0] = '/[\n\r]/';
            >
            > That searches for either a newline character or a carriage return.

            I'm told this will only work in Windows. That Macs would require a different
            string. Two, even.

            Anyone know if this is true?


            • 3. Re: - How do you refer to an HTML carriage return in PHP?
              Level 7
              Reese wrote:
              > "David Powers" <david@example.com> wrote in message
              >>
              >>$patterns[0] = '/[\n\r]/';
              >>
              >>That searches for either a newline character or a carriage return.
              >
              > I'm told this will only work in Windows. That Macs would require a different
              > string. Two, even.
              >
              > Anyone know if this is true?

              What you have been told is only partly correct. Windows uses a carriage
              return followed by a newline character (\r\n). Macs just use a newline
              character (\n).

              The pattern I have given you search for either a newline character or
              carriage return. So if it's applied in such a way as to remove all
              instances, it will work on both Windows and Mac. An alternative pattern
              which would also work on both is this:

              $patterns[0] = '/\r?\n/';

              It looks for an optional carriage return followed by a newline
              character. In other words, it still matches if there isn't a carriage
              return.

              --
              David Powers
              Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
              Author, "Foundation PHP 5 for Flash" (friends of ED)
              http://foundationphp.com/
              • 4. Re: - How do you refer to an HTML carriage return in PHP?
                Level 7
                "David Powers" <david@example.com> wrote in message
                news:e4hclo$qh4$1@forums.macromedia.com...
                > Reese wrote:
                >> "David Powers" <david@example.com> wrote in message
                >>>
                >>>$patterns[0] = '/[\n\r]/';
                >>>
                >>>That searches for either a newline character or a carriage return.
                >>
                >> I'm told this will only work in Windows. That Macs would require a
                >> different string. Two, even.
                >>
                >> Anyone know if this is true?
                >
                > What you have been told is only partly correct. Windows uses a carriage
                > return followed by a newline character (\r\n). Macs just use a newline
                > character (\n).
                >
                > The pattern I have given you search for either a newline character or
                > carriage return. So if it's applied in such a way as to remove all
                > instances, it will work on both Windows and Mac. An alternative pattern
                > which would also work on both is this:
                >
                > $patterns[0] = '/\r?\n/';
                >
                > It looks for an optional carriage return followed by a newline character.
                > In other words, it still matches if there isn't a carriage return.

                The response of my know-it-all roumanian programmer with an ego the size of
                Texas to that was :

                http://www.regular-expressions.info/characters.html (bottom of the page)

                \r\n = windows
                \n = Linux (not Mac)
                \r = Mac

                http://www.regular-expressions.info/optional.html (tells you what the '?'
                does)
                (it means that the character before it can be there 0 or 1 times)
                \r?\n = \n (Linux) or \r\n (windows)
                It leaves out the \r all alone, like added by the Mac.

                So, it will not delete the Mac new lines (might delete the Mac OSX ones
                since that one is passed on Linux, but not the older ones.)

                Now, you can contradict me all you want and have a busted code (then you
                will say it doesn't work because I made it wrong), or do it like I told you
                and get it working like it should.

                ----------------------

                To this, I told him I would present this to you for a rebuttal, but not
                before asking him why he used only the '/\r?\n/' model as an example, and
                not the recommended '/[\n\r]/'. Something tells me he has no idea what [
                and ] do in this context, so he's avoiding it altogether -- even though it
                may very well solve the problem on its own the way you claim it does.

                ;-)


                • 5. Re: - How do you refer to an HTML carriage return in PHP?
                  Level 7
                  Reese wrote:
                  > The response of my know-it-all roumanian programmer with an ego the size of
                  > Texas to that was :
                  >
                  > http://www.regular-expressions.info/characters.html (bottom of the page)
                  >
                  > \r\n = windows
                  > \n = Linux (not Mac)
                  > \r = Mac

                  He may be right about \r being used by Mac Classic OS, but the page that
                  he points to doesn't say anything about it.

                  > So, it will not delete the Mac new lines (might delete the Mac OSX ones
                  > since that one is passed on Linux, but not the older ones.)

                  Actually, Mac OS X is based on Unix, not Linux.

                  > To this, I told him I would present this to you for a rebuttal, but not
                  > before asking him why he used only the '/\r?\n/' model as an example, and
                  > not the recommended '/[\n\r]/'. Something tells me he has no idea what [
                  > and ] do in this context,

                  The answer is in the same website he pointed you to:

                  http://www.regular-expressions.info/charclass.html

                  Placing the square brackets around \n\r selects either \n or \r, but not
                  both. However, as long as the regular expression is used to remove all
                  instances of a pattern, it will remove \n every time it encounters it,
                  and \r every time it encounters it.

                  --
                  David Powers
                  Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
                  Author, "Foundation PHP 5 for Flash" (friends of ED)
                  http://foundationphp.com/
                  • 6. Re: - How do you refer to an HTML carriage return in PHP?
                    Level 7
                    "David Powers" <david@example.com> wrote in message
                    news:e4iaca$6q8$1@forums.macromedia.com...
                    > Reese wrote:
                    >> The response of my know-it-all roumanian programmer with an ego the size
                    >> of Texas to that was :
                    >>
                    >> http://www.regular-expressions.info/characters.html (bottom of the page)
                    >>
                    >> \r\n = windows
                    >> \n = Linux (not Mac)
                    >> \r = Mac
                    >
                    > He may be right about \r being used by Mac Classic OS, but the page that
                    > he points to doesn't say anything about it.
                    >
                    >> So, it will not delete the Mac new lines (might delete the Mac OSX ones
                    >> since that one is passed on Linux, but not the older ones.)
                    >
                    > Actually, Mac OS X is based on Unix, not Linux.
                    >
                    >> To this, I told him I would present this to you for a rebuttal, but not
                    >> before asking him why he used only the '/\r?\n/' model as an example, and
                    >> not the recommended '/[\n\r]/'. Something tells me he has no idea what
                    >> [ and ] do in this context,
                    >
                    > The answer is in the same website he pointed you to:
                    >
                    > http://www.regular-expressions.info/charclass.html
                    >
                    > Placing the square brackets around \n\r selects either \n or \r, but not
                    > both. However, as long as the regular expression is used to remove all
                    > instances of a pattern, it will remove \n every time it encounters it, and
                    > \r every time it encounters it.

                    What about '/[\n\r]+/' ..?

                    Would that include *both* as well, unlike the previous formula?


                    • 7. Re: - How do you refer to an HTML carriage return in PHP?
                      Level 7
                      Reese wrote:
                      > What about '/[\n\r]+/' ..?
                      >
                      > Would that include *both* as well, unlike the previous formula?

                      Yes, it would, but it's academic if you're using something like
                      preg_match_all() or preg_replace() because they both find all matches.

                      '/[\n\r]+/' finds \n, \r, \n\n, \n\n\n\n\n\n\r, \r\n, and a gazillion
                      other combinations.

                      '/[\n\r]/' finds just \n or \r, and is theoretically more efficient
                      because it doesn't have to keep backtracking.

                      --
                      David Powers
                      Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
                      Author, "Foundation PHP 5 for Flash" (friends of ED)
                      http://foundationphp.com/