Carlos Jhon manlulu Carlos Jhon manlulu - 4 months ago 28
PHP Question

how to remove white space in select 2

i have dynamically add the value of the option in select2 it came from database but when the field have no value the space can be selected how should i remove the spaces? i tried this codes but nothing happens

$imei=$row["IMEI_MX"];
$imeiserial = explode(',', $imei);
<select id="tags" name="imei" disabled class="form-control" onchange="getCount()" multiple>

foreach($imeiserial as $imeiserial){
$wew=ltrim($imeiserial, " \t.");

echo '
<option value='.$imei.'>'.$wew.'</option>';

[


]1

Answer

I'm not entirely sure what your general layout is, but if you're using PHP to generate the HTML select tag, then you can try something like this:

$imei = $row["IMEI_MX"];
$imeiserial = explode(',', $imei);

echo '<select id="tags" name="imei" disabled class="form-control" onchange="getCount()" multiple>';

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

    if (!empty($wew)) {
        echo "<option value='$wew'>$wew</option>";
    }
}
echo '</select>';

The PHP empty() function will return true if the variable is NULL, an empty string, or does not exist. This means that if the value is a space (or empty string once the trim() function has gone through, then it won't output a html <option> tag.

This is all assuming that you're meant to be creating a separate <option> for each element in the array after an explode (I'm guessing it's a comma-separated list of IMEIs) but you were outputting the entire string as the value rather than just the element.

e.g.

<option value='867637026628082, 867637026628090'>867637026628082</option>

rather than

<option value='867637026628082'>867637026628082</option>