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条)

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP