using trim()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

using trim()

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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.
Post Reply