ecwarrior ecwarrior - 15 days ago 5
PHP Question

Array explode has empty value for html tag <li> how to not include that value

I have a random id generator which could be anywhere from 1 to x number of ids

$ranGen = "IDENTIFIER | c0402347-8b93-49e4-991b-8213ea2921b1| e8087fc5-ded7-43ab-858d-127fe23f90bc|" ;


I'm trying to go through each value and present it as an un-ordered list

<?php
$ranGen = "IDENTIFIER | c0402347-8b93-49e4-991b-8213ea2921b1| e8087fc5-ded7-43ab-858d-127fe23f90bc|" ;
$array = explode("|", $ranGen);
echo '<ul>' ;
foreach ($array as $value) {
echo '<li>' . trim($value) . '</li>';
}
echo '</ul>';
?>


I get the following results

<ul>
<li>IDENTIFIER </li>
<li> c0402347-8b93-49e4-991b-8213ea2921b1 </li>
<li> e8087fc5-ded7-43ab-858d-127fe23f90bc </li>
<li></li>


I do not want the last blank list - is there a way to do this

Answer

Test the value before you echo:

foreach ($array as $value) {
    if( trim($value) != '') {
        echo '<li>' . trim($value) . '</li>';
    }
}