Jump to content

Why trim() does not work?

- - - - -

  • Please log in to reply
3 replies to this topic

#1
thatsme

thatsme

    Programmer

  • Members
  • PipPipPipPip
  • 176 posts
Hi. Let's say i have a form with textarea. Then I type 6 in the textarea and press Enter two times. After that i press submit and php code starts to run. It uses function trim() to remove the effect of those two pushes of Enter from textarea's value. Then php print the value of textarea and the result is: 6\r\n\r\n. WHY? I used trim(). Here's the code:

$commentText = mysql_real_escape_string($_POST['commentArea']);
$commentText = trim($commentText);


#2
Vaielab

Vaielab

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 547 posts
I'm not 100% sure, but try to trim before mysql_real_escape_string
Since mysql_real_escape_string will escape the \r\n chars, and trim are looking for thoses char unscaped, this might be the problem

#3
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200

php.net/mysql_real_escape_string said:

mysql_real_escape_string() calls MySQL's library function mysql_real_escape_string, which prepends backslashes to the following characters: \x00, \n, \r, \, ', " and \x1a.

This is to prevent expansion of the string, and thus render either malformed encoding during transportation, or malicious use less likely. You will need to follow Vaielab's advice.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#4
thatsme

thatsme

    Programmer

  • Members
  • PipPipPipPip
  • 176 posts
thank's. that helped




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users