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

相关推荐 更多相似问题