I have a form for use to enter...

But sometimes people like to add many return carriage, new lines.

So I want to strip it out.. The below code doesn't work. Why is that?

Is it because it's can't recogize as "hey \n\n\t"?

explode("%_%",trim($_POST["PROBLEM"], " \t \n \r \s") );

    well [man]trim/man only removes it from the end of the string. and by default trim() removes everything you specified (except \s which is a Perl RegEx element).

    I'm guessing that if you just take out the second argument, you should be fine.

     explode("%_%",trim($_POST["PROBLEM"]) );

    But after a few tests, it won't trim anything that is within alpha-numeric characters. So if your post looks like:

    "This is a test\nof a string that has\n\tsomething on a bunch of lines\n\n\n\n\n\n\n\t\t\t\t\t\t\t.  "

    Then the trim will only trim the last 2 spaces. If it looks like:

    "This is a test\nof a string that has\n\tsomething on a bunch of lines\n\n\n\n\n\n\n\t\t\t\t\t\t\t  "

    It will trim all breaks and spaces.

    If you use your trim() call, the "s" on the end of "lines" will be stripped as well.

      Actually, trim() removes leading and trailing whitespace, while rtrim() only removes trailing whitespace.

      Removing extra whitespace within a string is easy with [man]preg_replace/man, but only after you decide how exactly you want to handle them. For example, you might replace all consecutive whitespace with a single space, or all consecutive whitespace of the same character sequence with a single instance of that character sequence.

        Actually, trim() removes leading and trailing whitespace, while rtrim() only removes trailing whitespace.

        That's what I meant... stupid mental brain block....

          $stringA=trim($stringA);
          $stringA=str_replace("\s", ' ', $stringA);  
          $stringA=str_replace("\r", ' ', $stringA);
          $stringA=str_replace("\n", ' ', $stringA);
          $stringA=str_replace("\t", ' ', $stringA);

          From the above, that doesn't take out white spaces in between, such as

          "   a                                           A "
          

          will become

           
          "a                                             A"
          

            From the above, that doesn't take out white spaces in between, such as

            That's true, and you would have known that simply by reading the posts by bpat1434 and me.

            Since you didnt state how exactly you want to handle the whitespace, I shall provide you with an example that turns consecutive whitespace into a single space:

            // our example string
            $str = "   a                                           A ";
            // perform the whitespace replacement
            $str = preg_replace('/\s+/', ' ', trim($str));
            // print result
            echo $str;

              From the above, that doesn't take out white spaces in between, such as

              Of course not:
              1.) Trim works on the ends
              2.) str_replace REPLACES your search needle with whatever you want. In your example, you replace a space with a space. Kinda unnecessary right?

              But Laserlight is right, that preg_replace would work for removing consecutive spaces.

                Write a Reply...