从字符串中删除最后一个逗号(PHP/Wordpress)

问题描述:

我正在 Wordpress 中生成自定义帖子类型的术语列表,在此代码中,我在每个项目的末尾添加一个逗号以将其以列表格式分隔,我将如何消除最后一个逗号在添加或删除列表中的最后一个逗号时进行传播.

I am generating a list of the terms on a custom post type in Wordpress, in this code i add a comma to the end of each item to separate it in a list format, how would i either eliminate the last the comma from propagating on addition or remove the last comma on the list.

$terms = get_the_terms( $post->ID, 'clients' );
if ( $terms && ! is_wp_error( $terms ) ) :
$clients_list = array();
foreach ( $terms as $term ) {
    $clients_list[] = $term->name;
}
$clients = join( ", ", $clients_list );
$catTags .= "$clients, ";
endif;

我尝试了以下方法都没有成功;

I have tried the following to no success;

<em><?php $string = $catTags;
    echo preg_replace("/\,$/","",$catTags); ?></em>

您可以简单地:

rtrim($catTags, ', ');