trim array?

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
edhen
Forum Newbie
Posts: 21
Joined: Fri Jul 16, 2010 11:26 am
Location: Adelaide, Australia

trim array?

Post by edhen »

is there a way to trim the white spaces and perform normal string functions on an array without having to call each one individually? eg. i got my register form all named as an array 'required[fname]' etc. yet i'm not sure how to process these all at once which was kinda the main reason for putting them in an array... thanks
User avatar
lenton
Forum Commoner
Posts: 49
Joined: Sun Jun 20, 2010 6:45 am

Re: trim array?

Post by lenton »

Not really sure what you mean by your post but you're probably looking for the function 'foreach()':

Code: Select all

foreach ($arr as $key => $value)
{
  trim(required[$key]);
}
edhen
Forum Newbie
Posts: 21
Joined: Fri Jul 16, 2010 11:26 am
Location: Adelaide, Australia

Re: trim array?

Post by edhen »

thanks... i found a solution just before... i tried that way but i think it was causing the array to trim it as a whole or something (not too sure)

Code: Select all

foreach($_POST['required'] as $id=>$value){ 
	$_POST['required'][$id] = trim($value);
	$_POST['required'][$id] = strip_tags($value);
	}
this way seemed to work nicely...
Gargoyle
Forum Contributor
Posts: 130
Joined: Wed Jul 14, 2010 12:25 am

Re: trim array?

Post by Gargoyle »

note that you are throwing away the return value of trim()...
edhen
Forum Newbie
Posts: 21
Joined: Fri Jul 16, 2010 11:26 am
Location: Adelaide, Australia

Re: trim array?

Post by edhen »

thanks for that... i got alittle ahead of my self with out thinking... btw Ive been up all night trying to experiment in making my own registration form and process without guides for the first time and only calling for help when im really stuck...

i change the code to this

Code: Select all

	
foreach($_POST['required'] as $id=>$value){ 
	$value = trim($value);
	$_POST['required'][$id] = strip_tags($value);
	}
would that be the right way to go about it? as it seems to work now...
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: trim array?

Post by AbraCadaver »

Use array functions if you don't want to loop:

Code: Select all

$required = array_map('strip_tags', array_map('trim', $_POST['required']));
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
buckit
Forum Contributor
Posts: 169
Joined: Fri Jan 01, 2010 10:21 am

Re: trim array?

Post by buckit »

AbraCadaver wrote:Use array functions if you don't want to loop:

Code: Select all

$required = array_map('strip_tags', array_map('trim', $_POST['required']));
Ha! was just about to reply with something similar! Saw your replay to one of my posts yesterday about using array functions instead of loops and thought I would give it a go with this one! you beat me to it! :)
edhen
Forum Newbie
Posts: 21
Joined: Fri Jul 16, 2010 11:26 am
Location: Adelaide, Australia

Re: trim array?

Post by edhen »

thanks for the info... i had a glance at array_map on php.net but was hesitant on using it as i was unfamiliar with how exactly it work... but thanks for simplifying it for me in your example will be put to good use...
Post Reply