Jump to content

Apply Trim on Whole Array?


chaseman

Recommended Posts

Is there a way to apply the function trim() on a whole array, instead of applying it to the individual input fields one by one?

 

Here's the code:

$blurb1_title = array ( 
$_POST['blurb1_title1'], 
$_POST['blurb1_title2'],
$_POST['blurb1_title3'],
$_POST['blurb1_title4'],
$_POST['blurb1_title5']
);

$blurb2_title = array ( 
$_POST['blurb2_title1'], 
$_POST['blurb2_title2'],
$_POST['blurb2_title3'],
$_POST['blurb2_title4'],
$_POST['blurb2_title5']
);

$blurb3_title = array ( 
$_POST['blurb3_title1'], 
$_POST['blurb3_title2'],
$_POST['blurb3_title3'],
$_POST['blurb3_title4'],
$_POST['blurb3_title5']
);

 

All those $_POST variables are input fields, and I would like to apply trim() to them so the spaces get removed.

 

Is there an efficient way to do it without having to apply it all individually, if yes, what would be the correct syntax?

 

 

 

 

Link to comment
Share on other sites

Oops I accidentally posted my reply to the other thread. Well here's the reply again.

 

Thanks PFM,

 

I tried to implement it but I was not able to do so, I tried the following:

 

function blurb_titles ($all_titles) {

trim($all_titles);

}

array_walk ($blurb2_title, 'blurb_titles');

 

Which I added below the array list.

 

But this would only break my script.

Link to comment
Share on other sites

What I tried was

 

array_map (trim(), $blurb2_title);

 

Didn't think that your version will work too, but now that I look at it it seems obvious. Trim is no different than any other function, it's just global.

 

 

Thanks abra!

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.