Carlos Jhon manlulu Carlos Jhon manlulu - 3 months ago 8
PHP Question

how to remove whitespace from explode

how will i eliminate the white space when the value is empty? i used this codes i also try to trim it but it does not work how should i do this ? thank you in advance.

Here is the picture

enter image description here

$imeitransferserial = explode(',', $imeitransfer);
$imeiserial = explode(',', $imei);

foreach($imeiserial as $is){
$imeicode = trim($is);

if (in_array($is,$imeitransferserial)) {
$select = 'selected="selected" ';
else {
$select = "";
echo "<option ".$select."value='$imeicode'>".$imeicode."</option>";


A combination of array_map and array_filter, or possibly just array_filter, will do the trick.

array_map with trim will remove extra whitespaces:

$imeitransferserial = array_map('trim',$imeitransferserial );

array_filter will remove empty elements from the array:

$imeitransferserial = array_filter($imeitransferserial);

If you can have a value of 0 in the array, you will want to use strlen as a callback for array_filter:

$imeitransferserial = array_filter($imeitransferserial, 'strlen');

By default, array_filter will remove anything that evaluates to 0.