Web Analytics Made Easy -
StatCounter Trimming "Line Breaks" In Textfield - CodingForum

Announcement

Collapse
No announcement yet.

Trimming "Line Breaks" In Textfield

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Trimming "Line Breaks" In Textfield

    Hey, I am having this issue that I cannot seem to find a solution for. Basically I am storing information typed into a textfield in my database. The problem is that if you just holder enter in the text field creating a bunch of new lines, it will put all of that into the database. I have tried PHP's trim(), ltrim(), rtrim(), preg_replace() with a regex inside...none of them seem to solve this issue.

    They do replace the whitespace if I type "sdf " for example, but not if I type,

    "sdf




    "
    How can I trim all of that to just get "sdf" out of it? Thanks

  • #2
    Originally posted by Tanner8 View Post
    Hey, I am having this issue that I cannot seem to find a solution for. Basically I am storing information typed into a textfield in my database. The problem is that if you just holder enter in the text field creating a bunch of new lines, it will put all of that into the database. I have tried PHP's trim(), ltrim(), rtrim(), preg_replace() with a regex inside...none of them seem to solve this issue.

    They do replace the whitespace if I type "sdf " for example, but not if I type,

    "sdf




    "
    How can I trim all of that to just get "sdf" out of it? Thanks
    trim() will only remove breaks and whitespace at the start and end of your string. I'm not sure how it reacts with multiple breaks at the end of the string, but I'm guessing it will have to be a recursive call to get rid of them. Also, are your new lines definitely \n or \r characters? If your input is being formatted using something like <br /> then trim() etc. wont remove the breaks.

    If your code is being formatted with <br /> then you could do:
    PHP Code:
    str_replace('<br />'''$string); 
    Other wise, a recursive call would be something like the following:
    PHP Code:
    function recur_trim($string){
        
    $trim = array( '\n''\r'' ''\t''\0''\x0B' );
        
    $string trim($string);
        if(
    in_array(substr($string01), $trim) || in_array(substr($string, -1))){
            return 
    recur_trim($string);
        }
        else{
            return 
    $string;
        }

    This recursion of trim() might not be needed, but I've never used it before and the manual doesn't mention any inherint recursiveness. Someone correct me if I'm wrong. Also, that recursion isn't tested - will test it later on when I have the time.
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

    Comment


    • #3
      trim accepts a character list as second argument. Check the manual for more details.

      Comment


      • #4
        Originally posted by gvre View Post
        trim accepts a character list as second argument. Check the manual for more details.
        Yes it does, but by default it trims \n, \r and other characters from the end and the start of a string. It's not very likely that his text is being formatted using nl2br() but if it is, adding <br /> along with a character list to trim() will do the trick - you're right about that.

        However, just specifying a character list doesn't solve his problem of multiple line breaks not being trim()'d, which is why I provided my recursive function. I'm not actually entirely confident on how trim() works as I've never used it, but judging on the manual it isn't recursive and doesn't have a recursive parameter meaning that it will only trim the first and last substring only. So, to get a theoretically infinite number of line breaks trim()'d off the end or a start of a string, my recursive function will do that.
        Useful function to retrieve difference in times
        The best PHP resource
        A good PHP FAQ
        PLEASE remember to wrap your code in [PHP] tags.
        PHP Code:
        // Replace this
        if(isset($_POST['submitButton']))
        // With this
        if(!empty($_POST))
        // Then check for values/forms. Some IE versions don't send the submit button 
        Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

        Comment


        • #5
          http://codepad.org/y5KN6rNl - testing my function. Seems trim has some sort of recursive nature already, making my function redundant. Post your code, there is obviously something else wrong with your code.
          Useful function to retrieve difference in times
          The best PHP resource
          A good PHP FAQ
          PLEASE remember to wrap your code in [PHP] tags.
          PHP Code:
          // Replace this
          if(isset($_POST['submitButton']))
          // With this
          if(!empty($_POST))
          // Then check for values/forms. Some IE versions don't send the submit button 
          Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

          Comment


          • #6
            If trim isn't working for the OP, it's definitely just not used correctly.

            As for trim; it isn't recursive, its iterative:
            PHP Code:
            PHPAPI char *php_trim(char *cint lenchar *whatint what_lenzval *return_valueint mode TSRMLS_DC)
            {
                
            register int i;
                
            int trimmed 0;
                
            char mask[256];

                if (
            what) {
                    
            php_charmask((unsigned char*)whatwhat_lenmask TSRMLS_CC);
                } else {
                    
            php_charmask((unsigned char*)" \n\r\t\v\0"6mask TSRMLS_CC);
                }

                if (
            mode 1) {
                    for (
            0leni++) {
                        if (
            mask[(unsigned char)c[i]]) {
                            
            trimmed++;
                        } else {
                            break;
                        }
                    }
                    
            len -= trimmed;
                    
            += trimmed;
                }
                if (
            mode 2) {
                    for (
            len 1>= 0i--) {
                        if (
            mask[(unsigned char)c[i]]) {
                            
            len--;
                        } else {
                            break;
                        }
                    }
                }

                if (
            return_value) {
                    
            RETVAL_STRINGL(clen1);
                } else {
                    return 
            estrndup(clen);
                }
                return 
            "";
            }

            static 
            void php_do_trim(INTERNAL_FUNCTION_PARAMETERSint mode)
            {
                
            char *str;
                
            char *what NULL;
                
            int str_lenwhat_len 0;
                
                if (
            zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC"s|s", &str, &str_len, &what, &what_len) == FAILURE) {
                    return;
                }
                
                
            php_trim(strstr_lenwhatwhat_lenreturn_valuemode TSRMLS_CC);

            The actual trim methods just call the php_do_trim and provide it with the 'mode' (ie: ltrim, rtrim, and trim).

            So this will trim all the preceding and trailing characters that match the character list (default of " \n\r\t\v\0"), but is not designed to work in between words. This makes sense as if you have:
            Code:
            cat
            
            
            dog
            In a text area, you obviously want the spaces. If you do not, you can use something simple like preg_replace to match any \n{2,} and replace it with \n.
            PHP Code:
            header('HTTP/1.1 420 Enhance Your Calm'); 
            Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

            Comment


            • #7
              Hey, thanks for the replies, I actually just identified the problem. The problem was that I was escaping the string with 'mysql_real_escape_string' prior to any trimming. After escaping, the trim method no longer works as it should. I simply performed my trim before escaping and all works well. Thanks

              Comment


              • #8
                Originally posted by Tanner8 View Post
                Hey, thanks for the replies, I actually just identified the problem. The problem was that I was escaping the string with 'mysql_real_escape_string' prior to any trimming. After escaping, the trim method no longer works as it should. I simply performed my trim before escaping and all works well. Thanks
                Yes that will do it. MySQL_real_escape_string will escape a linefeed. Because of this, you are no longer matching a \n, you are matching a \\n. Using a sanitizing technique should be the last thing you do, and preferably only on the data actually being inserted (not stored back into the original variable).
                PHP Code:
                header('HTTP/1.1 420 Enhance Your Calm'); 
                Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

                Comment

                Working...
                X