我有一个随机id生成器,可以是1到x个id的任意位置
$ranGen = "IDENTIFIER | c0402347-8b93-49e4-991b-8213ea2921b1| e8087fc5-ded7-43ab-858d-127fe23f90bc|" ;
我正在尝试浏览每个值并将其显示为无序列表
<?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>';
?>
我得到以下结果
<ul>
<li>IDENTIFIER </li>
<li> c0402347-8b93-49e4-991b-8213ea2921b1 </li>
<li> e8087fc5-ded7-43ab-858d-127fe23f90bc </li>
<li></li>
我不想要最后一个空白列表 - 有没有办法做到这一点
答案 0 :(得分:2)
在回显之前测试值:
foreach ($array as $value) {
if( trim($value) != '') {
echo '<li>' . trim($value) . '</li>';
}
}
答案 1 :(得分:0)
您有两种选择:
<?php
$ranGen = "IDENTIFIER | c0402347-8b93-49e4-991b-8213ea2921b1| e8087fc5-ded7-43ab-858d-127fe23f90bc|" ;
$array = explode("|", $ranGen);
echo '<ul>';
foreach ($array as $value) {
// Option 1
if (trim($value) !== "") {
echo '<li>' . trim($value) . '</li>';
}
}
echo '</ul>';
// Option 2
$array = explode("|", $ranGen, -1);
echo '<ul>';
foreach ($array as $value) {
echo '<li>' . trim($value) . '</li>';
}
echo '</ul>';
?>
答案 2 :(得分:0)
替换此行:echo '<li>' . trim($value) . '</li>';
:echo trim($value) ? '<li>' . trim($value) . '</li>' : null;
答案 3 :(得分:0)
只是过滤掉它:
<div class="links">
<div class="link-section">
<div class="link-box">
<img src="images/slider/2.jpg" class="link-img" alt="Forum" />
<div class="link-txt-1"> <span> Test mikonim forum ro 1 2 3 4 atefe eshghame </span> </div>
</div>
</div>
<div class="link-section">
<div class="link-box">
<img src="images/slider/2.jpg" class="link-img" alt="Forum" />
<div class="link-txt-2"> <span> Test mikonim forum ro 1 2 3 4 6 alireza miodi atefe eshghe alirezas </span> </div>
</div>
</div>
<div class="link-section">
<div class="link-box">
<img src="images/slider/2.jpg" class="link-img" alt="Forum" />
<div class="link-txt-3"> <span> Test mikonim forum ro 1 2 3 4 </span> </div>
</div>
</div>
</div>