Trimming array elements in PHP

Several times I've had a user setting that was separated by commas that I needed to split out into an array. People, correctly, put a space after commas. But for the array the space shouldn't be there. Here's a quick tip to strip out those leading spaces.

PHP:
  1. $array = array_map('trim', $array);

array_map will step through the array members and apply the trim() function to each giving you a nice, clean array without leading spaces.

Question, Comments...

Do you have more questions. Please either leave a comment below or join us in our new forum.

Leave a Reply