john john john john - 3 months ago 11
PHP Question

how to trim white space in array in php

I have an array which contains some words. I want to get all words as unique part. so when I come to a whitespace , it's broken to a new. I mean , finally I want something like this:

$output[0]='Aug',
$output[1]='20',
$output[2]='2016'


first I want to explode it , then trim
between words . this is my code but unfortunately doesn't work.
Aug 20
come together. I want them individually.

<?php
$firs_arr=new array();
$first_arr='Aug 20, 2016 ';
$second=explode(',',$first_arr);
$output=array_map('trim',$second);

Answer

If you call your source $source, then $result below is the array you want:

//split into parts separated by all non-word characters
$result = preg_split('/\W+/',trim($source));

Live demo