Page 1 of 1

using trim()

Posted: Thu Nov 22, 2007 11:33 pm
by s.dot
This seems like a ridiculously easy question.. but I can't find a good way to use trim()...

I'd like to do this:

Code: Select all

if (!empty(trim($_POST['foo']))
But PHP won't let me do that. So I use:

Code: Select all

if (!empty($_POST['foo']) && (trim($_POST['foo'] != ''))
Is it always necessary to do it with the comparison operators? It just seems akward. If you don't use trim, a single space (or multiple) or other characters will be a value, so !empty() will return true.

Posted: Thu Nov 22, 2007 11:50 pm
by John Cartwright
You could use

Code: Select all

$_POST = array_map('trim', $_POST);
, better yet a callback function that can recursively trim the array.

Posted: Thu Nov 22, 2007 11:52 pm
by s.dot
Yeah, I suppose that's true. I can't think of a situation where I'd like to allow whitespace at the beginning or end of a string, besides passwords. Even then, if trim() is called when creating the password, and then for matching the hash, it will still match.

Posted: Fri Nov 23, 2007 3:40 am
by onion2k
scottayy wrote:Yeah, I suppose that's true. I can't think of a situation where I'd like to allow whitespace at the beginning or end of a string, besides passwords. Even then, if trim() is called when creating the password, and then for matching the hash, it will still match.
It'll also match if the user enters their password without the whitespace though, which is technically wrong.