How to remove last character from a string inside a loop

前端 未结 3 1892
再見小時候
再見小時候 2021-01-28 19:45

I am trying to print the different category\'s selected in a single line in xml, like

meeting, food and drinks, sports


        
相关标签:
3条回答
  • 2021-01-28 20:11

    Or use a rtrim, rtrim( $cat , ',' ) http://php.net/rtrim

    0 讨论(0)
  • 2021-01-28 20:20

    You can do this by [i used my variable]

    $str = substr($str,0, (strlen($str)-1));
    
    0 讨论(0)
  • 2021-01-28 20:33

    Call trim() with the optional second parameter as a ','

    trim($cat_name, ',')
    

    However, since you're doing this in a while loop, you should build an array then implode() it rather than building the string in the loop. This avoids the extra comma to begin with.

    $arr = array();
    while($row=mysql_fetch_array($e))
    {
      $arr[] = $row['cat_name'];
    }
    $cat_name = implode(",", $arr);
    // $cat_name is now "meeting, food and drinks, sports"
    
    0 讨论(0)
提交回复
热议问题