...

View Full Version : Trimming "Line Breaks" In Textfield



Tanner8
08-28-2011, 05:45 AM
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

BluePanther
08-28-2011, 08:33 AM
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:


str_replace('<br />', '', $string);

Other wise, a recursive call would be something like the following:


function recur_trim($string){
$trim = array( '\n', '\r', ' ', '\t', '\0', '\x0B' );
$string = trim($string);
if(in_array(substr($string, 0, 1), $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.

gvre
08-28-2011, 10:32 AM
trim (http://gr.php.net/trim) accepts a character list as second argument. Check the manual for more details.

BluePanther
08-28-2011, 12:27 PM
trim (http://gr.php.net/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.

BluePanther
08-28-2011, 12:59 PM
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.

Fou-Lu
08-28-2011, 05:21 PM
If trim isn't working for the OP, it's definitely just not used correctly.

As for trim; it isn't recursive, its iterative:


PHPAPI char *php_trim(char *c, int len, char *what, int what_len, zval *return_value, int mode TSRMLS_DC)
{
register int i;
int trimmed = 0;
char mask[256];

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

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

if (return_value) {
RETVAL_STRINGL(c, len, 1);
} else {
return estrndup(c, len);
}
return "";
}

static void php_do_trim(INTERNAL_FUNCTION_PARAMETERS, int mode)
{
char *str;
char *what = NULL;
int str_len, what_len = 0;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s", &str, &str_len, &what, &what_len) == FAILURE) {
return;
}

php_trim(str, str_len, what, what_len, return_value, mode 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:


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.

Tanner8
08-28-2011, 11:31 PM
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

Fou-Lu
08-29-2011, 01:40 AM
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).



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum