trim array?
Moderator: General Moderators
trim array?
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
Re: trim array?
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]);
}Re: trim array?
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)
this way seemed to work nicely...
Code: Select all
foreach($_POST['required'] as $id=>$value){
$_POST['required'][$id] = trim($value);
$_POST['required'][$id] = strip_tags($value);
}Re: trim array?
note that you are throwing away the return value of trim()...
Re: trim array?
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
would that be the right way to go about it? as it seems to work now...
i change the code to this
Code: Select all
foreach($_POST['required'] as $id=>$value){
$value = trim($value);
$_POST['required'][$id] = strip_tags($value);
}- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: trim array?
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.
Re: trim array?
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!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']));
Re: trim array?
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...