从字符串中删除最后一个逗号(PHP/Wordpress) [英] Remove the last Comma from a string (PHP / Wordpress)
本文介绍了从字符串中删除最后一个逗号(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, ', ');
这篇关于从字符串中删除最后一个逗号(PHP/Wordpress)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文