【问题标题】:remove last comma from string in while loop in php?在php的while循环中从字符串中删除最后一个逗号?
【发布时间】:2017-03-24 01:34:39
【问题描述】:

我正在使用 while 循环从我的数据库中获取值,结果如下:

我想在循环的最后一个值之后删除逗号。

我使用了类似的代码..

<?php
 $cat = get_terms('car_category'); // you can put your custom taxonomy name as place of category.
        foreach ($cat as $catVal) {
            echo '<b>'.$catVal->name.'</b>';
            $postArg = array('post_type'=>'cars','posts_per_page'=>5,'order'=>'desc',
                              'tax_query' => array(
                                                    array(
                                                        'taxonomy' => 'car_category',
                                                        'field' => 'term_id',
                                                        'terms' => $catVal->term_id
                                                    )
                            ));

            $getPost = new wp_query($postArg);
            global $post;

            if($getPost->have_posts()){
                $str = "";
                echo '<div class="yearDiv">';
                    while ( $getPost->have_posts()):$getPost->the_post();
                        $str =rtrim($post->post_title, ",");
                        echo '<span>'.$str.',</span>';
                    endwhile;
                echo '</div>';
            }
        }
?>

我也使用了 rtrim , substr 函数,但逗号没有删除。

【问题讨论】:

    标签: php string wordpress


    【解决方案1】:

    您是否尝试过在 while 循环中删除 rtrim,但在循环之后却创建了 rtrim 函数?像这样:

    echo '<div class="yearDiv">';
        while ( $getPost->have_posts()):$getPost->the_post();
            $str .= $post->post_title . ",";
        endwhile;
        echo '<span>'.rtrim($str, ",").'</span>';
    echo '</div>';
    

    【讨论】:

    • 是的,我试过了,但它只给了我循环的第一个值,比如.. Aprilia Dorsoduro 750 ABS,PRI PRIMODE,Yamaha XS650,Zero S,
    • 啊,是的,你必须使用 concat 作为字符串.. 粘合你所有的 $post->post_title.. 这次再试一次。
    【解决方案2】:

    为此使用 rtrim。只需确保在字符串空间的末尾 逗号后面不应该有。

       $yourString = rtrim($yourString, ",");
    

    【讨论】:

      【解决方案3】:

      将帖子标题放在一个数组中,然后改用 Implode ..

      <?php
       $cat = get_terms('car_category'); // you can put your custom taxonomy name as place of category.
              foreach ($cat as $catVal) {
                  echo '<b>'.$catVal->name.'</b>';
                  $postArg = array('post_type'=>'cars','posts_per_page'=>5,'order'=>'desc',
                                    'tax_query' => array(
                                                          array(
                                                              'taxonomy' => 'car_category',
                                                              'field' => 'term_id',
                                                              'terms' => $catVal->term_id
                                                          )
                                  ));
      
                  $getPost = new wp_query($postArg);
                  global $post;
      
                  if($getPost->have_posts()){
                      $str = array();
      
                          while ( $getPost->have_posts()):$getPost->the_post();
                              $str[] = $post->post_title;
      
                          endwhile;
                  }
                  echo '<div class="yearDiv">';
                  echo '<span>'.implode(', ', $str).'</span>';
                  echo '</div>'
              }
      ?>
      

      【讨论】:

        【解决方案4】:
        echo '<div class="yearDiv">';
        $str = "";
        while ( $getPost->have_posts()):$getPost->the_post();
            $str .= $post->post_title.",";
        endwhile;
        

        试试

        if(substr($str, -1) == ',') {
           $str = substr($str, 0, -1);
        }
        

        $str = rtrim($str,",");
        

        ...

        echo '<span>'.$str.'</span>';
        echo '</div>';
        

        【讨论】:

          【解决方案5】:

          php有修剪功能

          <?php
          $str = "item one ,item tow,item three,item 4,";
          echo trim($str, ",");
          ?>
          

          输出会是这样的

          item one ,item tow,item three,item 4
          

          【讨论】:

          • 不,输出将是这样的“item one item tow item 3 item 4”,因为它会从循环的每次迭代中删除逗号
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-02-14
          • 2013-11-24
          • 2013-02-17
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多