How to remove last character from a string inside a loop

前端 未结 3 1893
再見小時候
再見小時候 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: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"
    

提交回复
热议问题