2012-05-02 86 views
-4

可能重复:
Removing last comma in PHP?
Remove the last Comma from a string (PHP/Wordpress)摆脱最后一个逗号的

我有某种原因而不能正常工作的代码。我试图做的是摆脱结果中的最后一个逗号,但它不会工作。下面是代码

<?php do { 
    $activity_user_first_name = $row_product_activity['user_first_name']; 
    $activity_user_last_name = $row_product_activity['user_last_name']; 
    $name = '<a href="">'.$activity_user_first_name.' '.$activity_user_last_name.'</a>, '; 
    echo rtrim($name, ","); 
} while($row_product_activity = mysql_fetch_assoc($product_activity)) ?> like this 

所以结果我想是这样

Steve Jobs, Bill Gates, Sergey Brin, Larry Page like this 

,而不是我得到这个

Steve Jobs, Bill Gates, Sergey Brin, Larry Page, like this 

,所以我想拉里·佩奇或任何其他名称之后的逗号最后被删除。我尝试了许多其他选择,但没有按我的意愿出现。任何建议,将不胜感激。

+0

是的,我在发布我的问题之前阅读了其中的一些帖子,但他们都没有工作,所以这就是为什么我问及使用我的代码示例 –

回答

6

我认为一个更好的解决方案是首先构造没有拖尾逗号的字符串。将每个名称的链接放入数组槽中,然后仅回显一些内部爆炸的结果。

<?php 
    $names=array(); 
    do { 
     $activity_user_first_name = $row_product_activity['user_first_name']; 
     $activity_user_last_name = $row_product_activity['user_last_name']; 
     $names[] = '<a href="#">'.$activity_user_first_name.' '.$activity_user_last_name.'</a>'; 
    } while($row_product_activity = mysql_fetch_assoc($product_activity)); 

    echo implode(', ', $names);  

?> Like This