dro44817 2014-11-15 05:54
浏览 12
已采纳

如何从循环短语的开头和结尾删除空格

i need to remove whitespaces from the beginning and end of loop phrase

All words come from an loop, and look like this: " Hello all people "

I'm using the code -

$appliedFilters = Mage::getSingleton('catalog/layer')->getState()->getFilters();
foreach ($appliedFilters as $item) {
        if ($item->getFilter()->getRequestVar() == 'b_car' || 'c_model' || 'd_year') {
            $n_str = string.replace("\"", "", $item->getLabel()));
                   echo $n_str;
                    }
                    }

This code returns "Helloallpeople"

But i need "Hello all people"

Please help!

UPDATED

var_dump($item->getLabel()); returns string(7) "Hello " string(8) "all " string(5) "People "
  • 写回答

2条回答 默认 最新

  • dpp42324 2014-11-15 06:09
    关注

    Try Regx like the following:

    $returnValue = preg_replace("/>\s+(.*)\s+</", '>$1<', '<a> Hello all people <a/>');
    

    I am keeping the old answer for reference. But if you simply want to remove leading and trailing spaces; use trim()

    Update:

    If you want to trim each and every element of an array; you can map trim function to it. Then you can also implode the array to a string.

    <?php
    $str = array(" Hello all  ", " Hello all people ", "  all people ", " Hello people ");
    $n_str = array_map("trim",$str);
    var_dump($n_str);
    echo implode(" ",$n_str);
    ?>
    

    Update 2:

    Ok I got it; It's not an array. It's a loop. Every time $item->getLabel() returns just a string. it's not an array. Following should help you.

    $appliedFilters = Mage::getSingleton('catalog/layer')->getState()->getFilters();
    $result = "";
    foreach ($appliedFilters as $item) {
            if ($item->getFilter()->getRequestVar() == 'b_car' || 'c_model' || 'd_year') {
                $result .= " ".trim($item->getLabel());
            }
    }
    echo trim($result);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站