Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Aug 2009
    Posts
    51
    Thanks
    9
    Thanked 0 Times in 0 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
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Quote 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.

  • #3

  • #4
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Quote 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.

  • #5
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    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.

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.

  • #7
    New Coder
    Join Date
    Aug 2009
    Posts
    51
    Thanks
    9
    Thanked 0 Times in 0 Posts
    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

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote 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).


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •