php Php如何删除任何最后一个逗号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3120398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:44:30  来源:igfitidea点击:

Php how to remove any last commas

phptrim

提问by wow

Example output

示例输出

1. test,test,test,test,test,
2. test,test,test,,
3. test,test,,,
4. test,,,,,

I tried use implode according to my previous questionbut It's trim only last comma.

我尝试根据我之前的问题使用内爆,但它只修剪了最后一个逗号。

How to remove any last commas?

如何删除任何最后一个逗号?

回答by Artefacto

rtrim('test,,,,,', ',');

See the manual.

请参阅手册

回答by Kuchen

rtrimfor example:

rtrim例如:

$str = 'foo,bar,blah,bleh,';
echo rtrim($str,',');

// foo,bar,blah,bleh

回答by Ricky

completely different way:

完全不同的方式:

$str = "1,2,3,4,";
$str = substr($str,0,strlen($str)-1);