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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.