duanlv2788 2014-08-06 11:42
浏览 30
已采纳

使用PHP回显没有空格的字符串

I have a foreach loop with list of links, and I'm trying to remove the spaces in the strings because I'll be using it as an href. So far, I successfully removed the spaces, but the code I wrote made a wide space before the string. Here's my foreach loop

 <ul class="list grid effect-6" id="grid">
  <?php foreach ($ppmtenants as $ppmtenant): ?> 
         <li><span class="tenant"><a href="<?php echo $this->getURL() ?>catalogsearch/advanced/result/?tenants=<?php echo $ppmtenant['value'] ?>"><?php echo $ppmtenant['label'] ?></a><a href="<?php echo $this->getURL() ?>?___store=
    <?php 
    $ppmtenantnospace = preg_replace('/\s+/','',$ppmtenant['label']);
    echo strtolower($ppmtenantnospace); 
    ?>"></a></span></li>
<?php endforeach; ?>

    </ul>

And below is what I did to remove the spaces. I used preg_replace. Now, the spaces are gone, but it has a wide space before the string.

 <?php 
    $ppmtenantnospace = preg_replace('/\s+/','',$ppmtenant['label']);
    echo strtolower($ppmtenantnospace); 
    ?>

Live example here: http://powerplantv2.jehzlau.net/brands. Screenshot here: http://i.imgur.com/wpQwxso.png

As you can see, if you hover on the images, there's an unwanted space after the ?. There should be no space so that my permalinks will work. If that space disappears, my problem will be solved. But I don't know how to make it disappear. I hope a php expert here can help me. :(

  • 写回答

5条回答 默认 最新

  • douyalin2258 2014-08-06 11:46
    关注

    The problem is comming from the spaces in your html. To fix, simply move the variable declaration of $ppmtenantnospace above the li output, then echo it in at the correct place:

    <ul class="list grid effect-6" id="grid">
        <?php foreach ($ppmtenants as $ppmtenant): 
    
            $ppmtenantnospace = preg_replace('/\s+/','',$ppmtenant['label']);?>
    
            <li><span class="tenant"><a href="<?php echo $this->getURL() ?>catalogsearch/advanced/result/?tenants=<?php echo $ppmtenant['value'] ?>"><?php echo $ppmtenant['label'] ?></a><a href="<?php echo $this->getURL() ?>?___store=<?php echo $ppmtenantnospace;?>"></a></span></li>
    
        <?php endforeach; ?>
    
    </ul>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面